Comença a funcionar

This commit is contained in:
2024-04-09 14:30:47 +02:00
parent 84fdb68a70
commit d1982fc8b5
5 changed files with 183 additions and 14 deletions

84
z80.cpp
View File

@@ -1,10 +1,15 @@
#include "z80.h"
#include <stdio.h>
namespace z80
{
static uint8_t *memory = nullptr;
static uint32_t t = 0;
int (*in_ports[256])();
void (*out_ports[256])(int);
#define _rM16(a) (uint16_t*)&memory[a]
#define rM16(a) *_rM16(a)
@@ -18,7 +23,7 @@ namespace z80
#define fZ 0b01000000
#define fS 0b10000000
#define cNO -1
#define cNO 255
#define cNZ 0
#define cZ 1
#define cNC 2
@@ -124,6 +129,11 @@ namespace z80
#define SWAP(a,b) {auto temp=a;a=b;b=temp;}
uint16_t getPC()
{
return rPC;
}
uint8_t READ_MEM_8(const uint16_t addr)
{
t+=3;
@@ -143,7 +153,9 @@ namespace z80
uint16_t READ_MEM_16()
{
return READ_MEM_8() + READ_MEM_8() << 8;
const uint8_t L = READ_MEM_8();
const uint8_t H = READ_MEM_8();
return (H << 8) + L;
}
uint16_t READ_MEM_16(const uint16_t addr)
@@ -197,7 +209,7 @@ namespace z80
{
uint8_t count=0;
uint8_t f = rF;
while (f>0) { count+=(f&1); f>>1; }
while (f>0) { count+=(f&1); f=f>>1; }
if (!(count&1)) rF |= fP;
}
@@ -590,6 +602,7 @@ namespace z80
void HALT()
{
printf("HALT\n");
rPC--;
}
@@ -789,12 +802,19 @@ namespace z80
} else {
// don't touch flags
}
// [TODO] Do the actual "inputting"
if (in_ports[port])
return (uint8_t)in_ports[port]();
else
return 0x00;
}
void OUT(uint8_t port, uint8_t val)
void OUT(uint8_t val, int port = 256)
{
// [TODO]
t+=4;
if (port == 256) {
port = rC;
}
if (out_ports[port]) out_ports[port](val);
}
void INI()
@@ -833,15 +853,44 @@ namespace z80
}
}
void OUTI() { /* [TODO] */ }
void OUTIR() { /* [TODO] */ }
void OUTI()
{
OUT(READ_MEM_8(rHL));
rHL++;
DEC8(_rB);
t+=5;
}
void OUTDR() { /* [TODO] */ }
void OUTD() { /* [TODO] */ }
void OUTIR()
{
OUTI();
if (rB!=0)
{
rPC-=2;
t+=5;
}
}
void OUTD() {
OUT(READ_MEM_8(rHL));
rHL--;
DEC8(_rB);
t+=5;
}
void OUTDR() {
OUTD();
if (rB!=0)
{
rPC-=2;
t+=5;
}
}
void INVALID()
{
// [TODO]
printf("INVALID\n");
}
void reset(uint8_t* mem)
@@ -849,6 +898,11 @@ namespace z80
memory = mem;
rPC = 0;
t = 0;
for (int i=0; i<256; ++i)
{
in_ports[i] = nullptr;
out_ports[i] = nullptr;
}
}
void BIT_INSTRUCTIONS();
@@ -858,8 +912,15 @@ namespace z80
void IY_INSTRUCTIONS();
void IY_BIT_INSTRUCTIONS();
void step()
void connect_port(int num, int (*in_ptr)(), void (*out_ptr)(int))
{
if (in_ptr) in_ports[num] = in_ptr;
if (out_ptr) out_ports[num] = out_ptr;
}
uint32_t step()
{
t = 0;
const uint8_t opcode = READ_M1();
uint16_t tmp;
@@ -1137,6 +1198,7 @@ namespace z80
case 0xFE: CP(READ_MEM_8()); break;
case 0xFF: RST(0x38); break;
}
return t;
}
void BIT_INSTRUCTIONS()