Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Real IO for PicoMite #183

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ set(zxspectrum_common_src
${CMAKE_CURRENT_LIST_DIR}/ZxSpectrumMenu.cpp
${CMAKE_CURRENT_LIST_DIR}/ZxSpectrumFatSpiExists.cpp
${CMAKE_CURRENT_LIST_DIR}/PicoOnScreenKeyboard.cpp
${CMAKE_CURRENT_LIST_DIR}/ZxSpectrumPort.cpp
${CMAKE_CURRENT_LIST_DIR}/PicoMitePort.cpp
${CMAKE_CURRENT_LIST_DIR}/WavesharePiZeroPort.cpp
)

set(zxspectrum_rgb332_src
Expand Down Expand Up @@ -88,6 +91,7 @@ set(zxspectrum_common_libs
hardware_dma
hardware_flash # Needed for flash settings (not currently working)
hardware_sync # Needed for flash settings (not currently working)
hardware_adc
Zeta
Z80
)
Expand Down
32 changes: 32 additions & 0 deletions src/PicoMitePort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "PicoMitePort.h"
#include "hardware/adc.h"

// Output pins on GP10, GP11, GP12, GP13
static const uint8_t POUT_PINS[] = {10, 11, 12, 13};

void PicoMitePort::init() {
adc_init();

// Make sure GPIO is high-impedance, no pullups etc
adc_gpio_init(26);

// Select ADC input 2 (GP28)
adc_select_input(2);

for(size_t i = 0; i < sizeof(POUT_PINS); ++i) {
gpio_init(POUT_PINS[i]);
gpio_set_dir(POUT_PINS[i], GPIO_OUT);
}
}

uint8_t __not_in_flash_func(PicoMitePort::read)() {
uint16_t result = adc_read(); // Read 12 bit ADC
return (result >> 4) & 0xff;
}

void __not_in_flash_func(PicoMitePort::write)(uint8_t data) {
for(size_t i = 0; i < sizeof(POUT_PINS); ++i) {
gpio_put(POUT_PINS[i], (data & 1) == 1);
data >>= 1;
}
}
10 changes: 10 additions & 0 deletions src/PicoMitePort.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include "ZxSpectrumPort.h"

class PicoMitePort : public ZxSpectrumPort {
public:
uint8_t read();
void write(uint8_t data);
void init();
};
17 changes: 17 additions & 0 deletions src/RealPort.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#if defined(PICO_MITE_PORT)
#include "PicoMitePort.h"
static PicoMitePort picoMitePort;
#define ZX_SPECTRUM_REAL_PORT &picoMitePort
#define ZX_SPECTRUM_REAL_PORT_INIT picoMitePort.init();
#elif defined(WAVESHARE_PI_ZERO_PORT)
#include "WavesharePiZeroPort.h"
static WavesharePiZeroPort wavesharePiZeroPort;
#define ZX_SPECTRUM_REAL_PORT &wavesharePiZeroPort
#define ZX_SPECTRUM_REAL_PORT_INIT wavesharePiZeroPort.init();
#else
#define ZX_SPECTRUM_REAL_PORT 0
#define ZX_SPECTRUM_REAL_PORT_INIT
#endif

37 changes: 37 additions & 0 deletions src/WavesharePiZeroPort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "WavesharePiZeroPort.h"

// Output pins on GP9, GP10, GP11, GP12
static const uint8_t POUT_PINS[] = {9, 10, 11, 12};

// Input pins on GP13, GP14, GP15
static const uint8_t PIN_PINS[] = {13, 14, 15};

void WavesharePiZeroPort::init() {
for(size_t i = 0; i < sizeof(POUT_PINS); ++i) {
const uint8_t pin = POUT_PINS[i];
gpio_init(pin);
gpio_set_dir(pin, GPIO_OUT);
}
for(size_t i = 0; i < sizeof(PIN_PINS); ++i) {
const uint8_t pin = PIN_PINS[i];
gpio_init(pin);
gpio_set_dir(pin, GPIO_IN);
gpio_set_pulls(pin, true, false);
}
}

uint8_t __not_in_flash_func(WavesharePiZeroPort::read)() {
uint32_t data = 0;
for(size_t i = 0; i < sizeof(PIN_PINS); ++i) {
data |= gpio_get(PIN_PINS[sizeof(PIN_PINS) - i - 1]) ? 1 : 0;
data <<= 1;
}
return (uint8_t)data;
}

void __not_in_flash_func(WavesharePiZeroPort::write)(uint8_t data) {
for(size_t i = 0; i < sizeof(POUT_PINS); ++i) {
gpio_put(POUT_PINS[i], (data & 1) == 1);
data >>= 1;
}
}
10 changes: 10 additions & 0 deletions src/WavesharePiZeroPort.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include "ZxSpectrumPort.h"

class WavesharePiZeroPort : public ZxSpectrumPort {
public:
uint8_t read();
void write(uint8_t data);
void init();
};
4 changes: 3 additions & 1 deletion src/ZxSpectrum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ ZxSpectrum::ZxSpectrum(
ZxSpectrumKeyboard *keyboard1,
ZxSpectrumKeyboard *keyboard2,
ZxSpectrumJoystick *joystick,
ZxSpectrumMouse *mouse
ZxSpectrumMouse *mouse,
ZxSpectrumPort *parallelPort
) :
_moderate(9),
_keyboard1(keyboard1),
_keyboard2(keyboard2),
_joystick(joystick),
_mouse(mouse),
_parallelPort(parallelPort),
_borderColour(7),
_port254(0),
_ear(false),
Expand Down
17 changes: 14 additions & 3 deletions src/ZxSpectrum.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "hardware/pwm.h"
#include "ZxSpectrumAudio.h"
#include "ZxSpectrumMouse.h"
#include "ZxSpectrumPort.h"

// #define DEBUG_SPEC
#ifdef DEBUG_SPEC
Expand Down Expand Up @@ -43,6 +44,7 @@ class ZxSpectrum {
ZxSpectrumKeyboard *_keyboard2;
ZxSpectrumJoystick *_joystick;
ZxSpectrumMouse *_mouse;
ZxSpectrumPort *_parallelPort;
uint8_t _borderColour;
uint8_t _port254;
uint8_t _portMem;
Expand Down Expand Up @@ -137,6 +139,10 @@ class ZxSpectrum {
return _mouse->buttons();
}

if (address == 0x0FFF && _parallelPort) {
return _parallelPort->read();
}

return 0xFF;
}

Expand All @@ -156,7 +162,7 @@ inline void writeIO(uint16_t address, uint8_t value)
return;
} //BFFD

if (!(address & 0x8002) && _type != ZxSpectrum48k)
if (!(address & 0x8002) && (_type != ZxSpectrum48k))
{
if ((_portMem & 0x20) == 0) {
_fc += ((_portMem ^ value) >> 3) & 1;
Expand All @@ -165,7 +171,11 @@ inline void writeIO(uint16_t address, uint8_t value)
setPageaddr(0, (uint8_t*)((value & 0x10) ? zx_128k_rom_2 : zx_128k_rom_1));
}
return;
};
};

if (address == 0x0FFF && _parallelPort) {
_parallelPort->write(value);
}
}
else
{
Expand Down Expand Up @@ -243,7 +253,8 @@ inline void writeIO(uint16_t address, uint8_t value)
ZxSpectrumKeyboard *keyboard1,
ZxSpectrumKeyboard *keyboard2,
ZxSpectrumJoystick *joystick,
ZxSpectrumMouse *mouse
ZxSpectrumMouse *mouse,
ZxSpectrumPort *parallelPort
);
inline uint8_t* screenPtr() { return (unsigned char*)&_RAM[(_portMem & 8) ? 7 : 5]; }
inline uint8_t* memPtr(uint32_t i) { return (unsigned char*)&_RAM[i]; }
Expand Down
2 changes: 2 additions & 0 deletions src/ZxSpectrumPort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include "ZxSpectrumPort.h"

9 changes: 9 additions & 0 deletions src/ZxSpectrumPort.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once
#include <pico/stdlib.h>

class ZxSpectrumPort {
public:
virtual uint8_t __not_in_flash_func(read)() = 0;
virtual void __not_in_flash_func(write)(uint8_t data) = 0;
virtual void init();
};
3 changes: 3 additions & 0 deletions src/hdmi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ foreach(target
add_executable(${target}
${zxspectrum_common_src}
${zxspectrum_hdmi_src}
main.cpp
)

target_link_libraries(${target}
Expand Down Expand Up @@ -386,6 +387,8 @@ set (ZxSpectrumPiZero_defines
SDCARD_PIN_SPI0_MISO=20
# HDMI audio
PICO_HDMI_AUDIO
# Add a real IO port
WAVESHARE_PI_ZERO_PORT
)

target_compile_definitions(ZxSpectrumPiZero_640x480x60Hz PRIVATE
Expand Down
5 changes: 4 additions & 1 deletion src/hdmi/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ extern "C" {
#include "ZxSpectrumAudio.h"
#include "ZxSpectrumFileSettings.h"
#include "ZxSpectrumDisplay.h"
#include "RealPort.h"

#define UART_ID uart0
#define BAUD_RATE 115200
Expand Down Expand Up @@ -109,7 +110,8 @@ static ZxSpectrum zxSpectrum(
&keyboard1,
&keyboard2,
&joystick,
&mouse
&mouse,
ZX_SPECTRUM_REAL_PORT
);
static ZxSpectrumFileSettings zxSpectrumSettings(
&sdCard0,
Expand Down Expand Up @@ -379,6 +381,7 @@ int main() {
#ifdef USE_PS2_KBD
ps2kbd.init_gpio();
#endif
ZX_SPECTRUM_REAL_PORT_INIT

gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
Expand Down
3 changes: 2 additions & 1 deletion src/picomputer/picomputer_st7789/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ static ZxSpectrum zxSpectrum(
&keyboard1,
&keyboard2,
&dualJoystick,
&mouse
&mouse,
0
);
static ZxSpectrumFileSettings zxSpectrumSettings(
&sdCard0,
Expand Down
3 changes: 2 additions & 1 deletion src/picomputer/picomputer_vga/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ static ZxSpectrum zxSpectrum(
&keyboard1,
&keyboard2,
&dualJoystick,
&mouse
&mouse,
0
);
static ZxSpectrumFileSettings zxSpectrumSettings(
&sdCard0,
Expand Down
3 changes: 2 additions & 1 deletion src/picomputer/picomputer_vga_zx/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ static ZxSpectrum zxSpectrum(
&keyboard1,
&keyboard2,
&dualJoystick,
&mouse
&mouse,
0
);
static ZxSpectrumFileSettings zxSpectrumSettings(
&sdCard0,
Expand Down
3 changes: 2 additions & 1 deletion src/picovga/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ static ZxSpectrum zxSpectrum(
&keyboard1,
0,
&joystick,
&mouse
&mouse,
0
);
static ZxSpectrumFileSettings zxSpectrumSettings(
&sdCard0,
Expand Down
3 changes: 3 additions & 0 deletions src/vga/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ set(ZxSpectrumPicoMiteVGA_defines
USE_MRMLTR_PS2_KBD
# Use stdio
USE_STDIO
# Add some real IO
PICO_MITE_PORT
)

foreach(target
Expand Down Expand Up @@ -212,6 +214,7 @@ foreach(target
tinyusb_board
hardware_pio
hardware_pwm
hardware_adc
)

pico_generate_pio_header(${target}
Expand Down
7 changes: 5 additions & 2 deletions src/vga/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "ZxScanlineVgaRenderLoop.h"
#include "ZxSpectrumNespadJoystick.h"
#include "hid_app.h"
#include "RealPort.h"

#define VREG_VSEL VREG_VOLTAGE_1_20

Expand Down Expand Up @@ -78,7 +79,8 @@ static ZxSpectrum zxSpectrum(
&keyboard1,
0,
&joystick,
&mouse
&mouse,
ZX_SPECTRUM_REAL_PORT
);
static ZxSpectrumFileSettings zxSpectrumSettings(
&sdCard0,
Expand Down Expand Up @@ -313,7 +315,8 @@ int main(){
#ifdef USE_PS2_KBD
ps2kbd.init_gpio(); // pio1, SM ?
#endif

ZX_SPECTRUM_REAL_PORT_INIT

// Configure the GPIO pins for audio
zxSpectrumAudioInit(); // pio1, ear SM ?, I2S SM ?

Expand Down