1
0
mirror of https://github.com/jscrane/r65emu.git synced 2024-06-11 13:29:34 +00:00

remove cruft

This commit is contained in:
Stephen Crane 2016-01-23 21:15:14 +00:00
parent 6f44a9e17b
commit 8dbba19309
7 changed files with 315 additions and 359 deletions

17
CPU.h
View File

@ -1,10 +1,6 @@
#ifndef __CPU_H__
#define __CPU_H__
#ifndef _SETJMP_H
#include <setjmp.h>
#endif
#undef PC
class CPU: public Checkpointable {
@ -12,21 +8,18 @@ public:
virtual void run(unsigned instructions) =0;
virtual void reset() =0;
virtual void raise(int level) =0;
virtual char *status () =0;
typedef void (*statfn) (const char *, ...);
virtual char *status(char *buf, size_t n) =0;
virtual void checkpoint(Stream &s) = 0;
virtual void restore(Stream &s) = 0;
void debug() { _debug = !_debug; }
inline void debug() { _debug = !_debug; }
inline bool halted() { return _halted; }
protected:
CPU (Memory &mem, jmp_buf &e, statfn s): _mem(mem), _err(e), _status(s), _debug(false) {}
CPU(Memory &mem): _mem(mem), _debug(false), _halted(false) {}
Memory &_mem;
Memory::address PC;
jmp_buf &_err;
statfn _status;
bool _debug;
bool _debug, _halted;
};
#endif

View File

@ -5,29 +5,14 @@
#include "ports.h"
#include "i8080.h"
#define CPU_STATE_FMT "%04x %02x %02x %04x %04x %04x %04x %d%d%d%d%d%d%d%d\r",\
PC, op, A, BC, DE, HL, SP, flags.S, flags.Z,\
flags.I, flags.H, flags._, flags.P, flags.__, flags.C
void i8080::step() {
void i8080::run(unsigned clocks) {
while (clocks--) {
byte op = _mem[PC];
#if defined(CPU_DEBUG)
if (_debug)
_status(CPU_STATE_FMT);
#endif
PC++;
(this->*_ops[op])();
if (_halted)
break;
}
void i8080::run(unsigned clocks) {
#if defined(CPU_DEBUG)
if (_debug) {
step();
return;
}
#endif
while (clocks--)
step();
}
void i8080::reset() {
@ -35,6 +20,7 @@ void i8080::reset() {
_sr(0);
BC = DE = HL = PC = SP = 0;
_irq_pending = 0;
_halted = false;
}
void i8080::raise(int level) {
@ -53,10 +39,12 @@ void i8080::ei() {
raise(_irq_pending);
}
char *i8080::status() {
static char buf[128];
char *i8080::status(char *buf, size_t n) {
byte op = _mem[PC];
sprintf(buf, "_pc_ op aa _bc_ _de_ _hl_ _sp_ szih_p_c\r" CPU_STATE_FMT);
snprintf(buf, n, "_pc_ op aa _bc_ _de_ _hl_ _sp_ szih_p_c\r"
"%04x %02x %02x %04x %04x %04x %04x %d%d%d%d%d%d%d%d\r",
PC, op, A, BC, DE, HL, SP, flags.S, flags.Z, flags.I, flags.H,
flags._, flags.P, flags.__, flags.C);
return buf;
}
@ -94,11 +82,6 @@ void i8080::daa() {
flags.C = c;
}
void i8080::hlt() {
_status("CPU halted at %04x\r%s", (PC-1), status());
longjmp(_err, 1);
}
int i8080::parity_table[] = {
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
@ -118,7 +101,7 @@ int i8080::parity_table[] = {
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
};
i8080::i8080(Memory &m, jmp_buf &jb, CPU::statfn s, PortDevice<i8080> &d): CPU(m, jb, s)
i8080::i8080(Memory &m, PortDevice<i8080> &d): CPU(m)
{
_ports = &d;

View File

@ -6,12 +6,12 @@
class i8080: public CPU {
public:
i8080(Memory &, jmp_buf &, CPU::statfn, PortDevice<i8080> &);
i8080(Memory &, PortDevice<i8080> &);
void run(unsigned);
void reset();
void raise(int);
char *status();
char *status(char *buf, size_t n);
void checkpoint(Stream &);
void restore(Stream &);
@ -29,8 +29,6 @@ public:
inline byte sr() { return SR; }
private:
inline void step();
byte A;
union {
struct { byte C, B; };
@ -232,7 +230,7 @@ private:
void movme() { _mem[HL] = E; }
void movmh() { _mem[HL] = H; }
void movml() { _mem[HL] = L; }
void hlt();
void hlt() { _halted = true; PC--; }
void movma() { _mem[HL] = A; }
void movab() { A = B; }

View File

@ -5,31 +5,14 @@
#include "CPU.h"
#include "r6502.h"
#define CPU_STATE_FMT "%02x %02x %02x %02x %d%d%d%d%d%d%d%d %04x\r",\
A,X,Y,S,P.bits.N,P.bits.V,P.bits._,P.bits.B,\
P.bits.D,P.bits.I,P.bits.Z,P.bits.C,PC
inline void r6502::step() {
void r6502::run(unsigned clocks) {
while (clocks--) {
byte op = _mem[PC];
#ifdef CPU_DEBUG
if (_debug) {
flags();
_status(CPU_STATE_FMT);
}
#endif
PC++;
(this->*_ops[op])();
if (_halted)
break;
}
void r6502::run(unsigned clocks) {
#ifdef CPU_DEBUG
if (_debug) {
step();
return;
}
#endif
while (clocks--)
step();
}
byte r6502::flags() {
@ -38,13 +21,16 @@ byte r6502::flags() {
P.bits.Z = !Z;
P.bits.C = C;
P.bits._ = 1;
return P.value;
return P.flags;
}
char *r6502::status () {
static char buf[128];
char *r6502::status(char *buf, size_t n) {
flags();
sprintf (buf, "aa xx yy sp nv_bdizc _pc_\r" CPU_STATE_FMT);
snprintf(buf, n, "aa xx yy sp nv_bdizc _pc_\r"
"%02x %02x %02x %02x %d%d%d%d%d%d%d%d %04x\r",
A, X, Y, S, P.bits.N, P.bits.V, P.bits._, P.bits.B,
P.bits.D, P.bits.I, P.bits.Z, P.bits.C, PC);
return buf;
}
@ -63,7 +49,7 @@ void r6502::checkpoint(Stream &s)
s.write(I);
s.write(Z);
s.write(C);
s.write(P.value);
s.write(P.flags);
}
void r6502::restore(Stream &s)
@ -81,7 +67,7 @@ void r6502::restore(Stream &s)
I = s.read();
Z = s.read();
C = s.read();
P.value = s.read();
P.flags = s.read();
}
void r6502::raise(int level) {
@ -141,7 +127,7 @@ void r6502::php () {
}
void r6502::plp() {
P.value = popb ();
P.flags = popb();
N = P.bits.N? 0x80: 0;
V = P.bits.V;
Z = !P.bits.Z;
@ -185,14 +171,14 @@ void r6502::sbcd (byte d) {
}
void r6502::ill() {
_status("Illegal instruction at %04x!\r\n%s", (PC-1), status());
longjmp(_err, 1);
--PC;
_halted = true;
}
void r6502::reset()
{
_debug = false;
P.value = 0;
_debug = _halted = false;
P.flags = 0;
P.bits._ = 1;
P.bits.B = 1;
_irq = false;
@ -200,7 +186,7 @@ void r6502::reset()
PC = vector(resvec);
}
r6502::r6502 (Memory &m, jmp_buf &e, CPU::statfn s): CPU (m,e,s) {
r6502::r6502(Memory &m): CPU(m) {
for (int i=0; i < 256; i++) {
_fromBCD[i] = ((i >> 4) & 0x0f)*10 + (i & 0x0f);

View File

@ -9,11 +9,11 @@ public:
void raise(int);
void reset();
void run(unsigned);
char *status();
char *status(char *buf, size_t n);
void checkpoint(Stream &);
void restore(Stream &);
r6502 (Memory &, jmp_buf &, CPU::statfn);
r6502(Memory &);
private:
/* registers */
byte S, A, X, Y;
@ -29,12 +29,11 @@ private:
unsigned V:1;
unsigned N:1;
} bits;
byte value;
byte flags;
} P;
byte _toBCD[256], _fromBCD[256]; // BCD maps
bool _irq; // interrupt pending?
void step();
void irq();
void nmi();
byte flags();
@ -55,7 +54,7 @@ private:
inline Memory::address popa() {
byte b = popb();
return ((popb () << 8) | b);
return (popb() << 8) | b;
}
static const Memory::address nmivec = 0xfffa;

33
z80.cpp
View File

@ -5,12 +5,9 @@
#include "CPU.h"
#include "z80.h"
#define CPU_STATE_FMT
char *z80::status() {
static char buf[256];
char *z80::status(char *buf, size_t n) {
byte op = _mem[PC];
sprintf(buf, "_pc_ op _af_ _bc_ _de_ _hl_ _af' _bc' _de' _hl' _ir_ imff "
snprintf(buf, n, "_pc_ op _af_ _bc_ _de_ _hl_ _af' _bc' _de' _hl' _ir_ imff "
"_sp_ sz5h3pnc\r\n"
"%04x %02x %04x %04x %04x %04x %04x %04x %04x %04x %04x %d%d%d "
"%04x %d%d%d%d%d%d%d%d\r\n",
@ -67,7 +64,7 @@ byte z80::_fetch_op() {
_mc(PC, 4);
byte op = _mem[PC];
#if defined(CPU_DEBUG)
_status("%5d MR %04x %02x\n", _ts, PC, op);
printf("%5d MR %04x %02x\n", _ts, PC, op);
#endif
PC++;
R++;
@ -76,15 +73,11 @@ byte z80::_fetch_op() {
void z80::run(unsigned clocks) {
while (clocks--) {
step();
#if !defined(CPU_DEBUG)
if (_halted) {
_status("CPU halted at %04x\r\n%s", PC, status());
longjmp(_err, 1);
}
#endif
if (_irq_pending && _iff1)
if (_irq_pending)
_handle_interrupt();
step();
if (_halted)
break;
}
}
@ -100,13 +93,18 @@ void z80::reset() {
}
void z80::_handle_interrupt() {
_iff1 = false;
if (_irq_pending < 0 || _iff1) {
if (_halted) {
_halted = false;
PC++;
}
_push(PC);
if (_irq_pending < 0) { // NMI
_iff2 = _iff1;
PC = 0x0066;
ts(11);
} else {
_iff2 = false;
_iff1 = _iff2 = false;
R++;
if (_im == 0 || _im == 1)
PC = 0x0038;
@ -114,6 +112,7 @@ void z80::_handle_interrupt() {
PC = _rw(_irq_pending + (0x100 * I));
ts(7);
}
}
_irq_pending = 0;
}
@ -871,7 +870,7 @@ int z80::parity_table[] = {
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
};
z80::z80(Memory &m, jmp_buf &jb, CPU::statfn s, PortDevice<z80> &ports): CPU(m, jb, s)
z80::z80(Memory &m, PortDevice<z80> &ports): CPU(m)
{
_ports = &ports;
_debug = false;

6
z80.h
View File

@ -6,12 +6,12 @@
class z80: public CPU {
public:
z80(Memory &, jmp_buf &, CPU::statfn, PortDevice<z80> &);
z80(Memory &, PortDevice<z80> &);
void run(unsigned);
void reset();
void raise(int level) { _irq_pending = level; }
char *status();
char *status(char *buf, size_t n);
void checkpoint(Stream &);
void restore(Stream &);
@ -38,7 +38,6 @@ public:
inline word iy() { return IY; }
inline word sp() { return SP; }
inline word pc() { return PC; }
inline bool halted() { return _halted; }
inline bool iff1() { return _iff1; }
inline bool iff2() { return _iff2; }
inline byte im() { return _im; }
@ -137,7 +136,6 @@ private:
bool _iff1, _iff2;
unsigned long _ts;
bool _halted;
int _irq_pending;
PortDevice<z80> *_ports;