- [NEW] Port function pointers now also receive port number as uint16_t

- [NEW] Implemented keyboard response to IN(0xfe), not working yet, DKW
This commit is contained in:
2024-04-15 14:32:18 +02:00
parent 2781a50228
commit 045a2238bd
4 changed files with 100 additions and 19 deletions

31
z80.cpp
View File

@@ -8,8 +8,8 @@ namespace z80
static uint8_t *memory = nullptr;
static uint32_t t = 0;
int (*in_ports[256])();
void (*out_ports[256])(int);
int (*in_ports[256])(int);
void (*out_ports[256])(int, int);
#define _rM16(a) (uint16_t*)&memory[a]
#define rM16(a) *_rM16(a)
@@ -811,28 +811,33 @@ namespace z80
}
}
const uint8_t IN(int port = 256)
const uint8_t IN(int port = 0x10000)
{
t+=4;
if (port == 256) {
port = rC;
if (port == 0x10000) {
port = rBC;
// [TODO] set flags acordingly
} else {
port = (rA<<8) | port;
// don't touch flags
}
if (in_ports[port])
return (uint8_t)in_ports[port]();
else
if (in_ports[port]) {
const uint8_t val = (uint8_t)in_ports[port&0xff](port);
if (val != 0xff)
return val;
else
return 0xff;
} else
return 0x00;
}
void OUT(uint8_t val, int port = 256)
void OUT(uint8_t val, int port = 0x10000)
{
t+=4;
if (port == 256) {
port = rC;
if (port == 0x10000) {
port = rBC;
}
if (out_ports[port]) out_ports[port](val);
if (out_ports[port&0xff]) out_ports[port&0xff](port, val);
}
void INI()
@@ -931,7 +936,7 @@ namespace z80
void IY_INSTRUCTIONS();
void IY_BIT_INSTRUCTIONS();
void connect_port(int num, int (*in_ptr)(), void (*out_ptr)(int))
void connect_port(int num, int (*in_ptr)(int), void (*out_ptr)(int,int))
{
if (in_ptr) in_ports[num] = in_ptr;
if (out_ptr) out_ports[num] = out_ptr;

2
z80.h
View File

@@ -5,7 +5,7 @@
namespace z80
{
void reset(uint8_t* mem);
void connect_port(int num, int (*in_ptr)(), void (*out_ptr)(int));
void connect_port(int num, int (*in_ptr)(int), void (*out_ptr)(int,int));
void interrupt();
uint32_t step();

View File

@@ -1,4 +1,5 @@
#include "zx_ula.h"
#include <SDL2/SDL.h>
namespace zx_ula
{
@@ -6,12 +7,87 @@ namespace zx_ula
static uint8_t ear = 0;
static uint8_t mic = 0;
int port_in()
int port_in(int port)
{
return 0;
const uint8_t *keys = SDL_GetKeyboardState(NULL);
const uint8_t h_addr = (port>>8);
uint8_t result = 0xff;
if (!(h_addr & ~0xfe))
{
result &= ~(
keys[SDL_SCANCODE_LSHIFT]*0xef +
keys[SDL_SCANCODE_Z]*0xf7 +
keys[SDL_SCANCODE_X]*0xfb +
keys[SDL_SCANCODE_C]*0xfd +
keys[SDL_SCANCODE_V]*0xfe );
}
if (!(h_addr & ~0xfd))
{
result &= ~(
keys[SDL_SCANCODE_A]*0xef +
keys[SDL_SCANCODE_S]*0xf7 +
keys[SDL_SCANCODE_D]*0xfb +
keys[SDL_SCANCODE_F]*0xfd +
keys[SDL_SCANCODE_G]*0xfe );
}
if (!(h_addr & ~0xfb))
{
result &= ~(
keys[SDL_SCANCODE_Q]*0xef +
keys[SDL_SCANCODE_W]*0xf7 +
keys[SDL_SCANCODE_E]*0xfb +
keys[SDL_SCANCODE_R]*0xfd +
keys[SDL_SCANCODE_T]*0xfe );
}
if (!(h_addr & ~0xf7))
{
result &= ~(
keys[SDL_SCANCODE_1]*0xef +
keys[SDL_SCANCODE_2]*0xf7 +
keys[SDL_SCANCODE_3]*0xfb +
keys[SDL_SCANCODE_4]*0xfd +
keys[SDL_SCANCODE_5]*0xfe );
}
if (!(h_addr & ~0xef))
{
result &= ~(
keys[SDL_SCANCODE_0]*0xef +
keys[SDL_SCANCODE_9]*0xf7 +
keys[SDL_SCANCODE_8]*0xfb +
keys[SDL_SCANCODE_7]*0xfd +
keys[SDL_SCANCODE_6]*0xfe );
}
if (!(h_addr & ~0xdf))
{
result &= ~(
keys[SDL_SCANCODE_P]*0xef +
keys[SDL_SCANCODE_O]*0xf7 +
keys[SDL_SCANCODE_I]*0xfb +
keys[SDL_SCANCODE_U]*0xfd +
keys[SDL_SCANCODE_Y]*0xfe );
}
if (!(h_addr & ~0xbf))
{
result &= ~(
keys[SDL_SCANCODE_RETURN]*0xef +
keys[SDL_SCANCODE_L]*0xf7 +
keys[SDL_SCANCODE_K]*0xfb +
keys[SDL_SCANCODE_J]*0xfd +
keys[SDL_SCANCODE_H]*0xfe );
}
if (!(h_addr & ~0x7f))
{
result &= ~(
keys[SDL_SCANCODE_SPACE]*0xef +
keys[SDL_SCANCODE_RSHIFT]*0xf7 +
keys[SDL_SCANCODE_M]*0xfb +
keys[SDL_SCANCODE_N]*0xfd +
keys[SDL_SCANCODE_B]*0xfe );
}
return result;
}
void port_out(int val)
void port_out(int port, int val)
{
border_color = val & 0x7;
mic = (val>>3)&0x1;

View File

@@ -4,8 +4,8 @@
namespace zx_ula
{
int port_in();
void port_out(int val);
int port_in(int port);
void port_out(int port, int val);
uint8_t get_border_color();
}