2020-12-07 11:50:57 +11:00
|
|
|
/* Common code between all z80 emulators
|
2019-12-03 08:35:49 +11:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include "emul.h"
|
|
|
|
|
|
|
|
static Machine m;
|
2020-03-30 13:13:54 +11:00
|
|
|
static ushort traceval = 0;
|
2019-12-03 08:35:49 +11:00
|
|
|
|
|
|
|
static uint8_t io_read(int unused, uint16_t addr)
|
|
|
|
{
|
|
|
|
addr &= 0xff;
|
|
|
|
IORD fn = m.iord[addr];
|
|
|
|
if (fn != NULL) {
|
|
|
|
return fn();
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Out of bounds I/O read: %d\n", addr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void io_write(int unused, uint16_t addr, uint8_t val)
|
|
|
|
{
|
|
|
|
addr &= 0xff;
|
|
|
|
IOWR fn = m.iowr[addr];
|
|
|
|
if (fn != NULL) {
|
|
|
|
fn(val);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Out of bounds I/O write: %d / %d (0x%x)\n", addr, val, val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t mem_read(int unused, uint16_t addr)
|
|
|
|
{
|
|
|
|
return m.mem[addr];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mem_write(int unused, uint16_t addr, uint8_t val)
|
|
|
|
{
|
|
|
|
if (addr < m.ramstart) {
|
|
|
|
fprintf(stderr, "Writing to ROM (%d)!\n", addr);
|
2020-04-09 22:26:41 +10:00
|
|
|
emul_memdump();
|
|
|
|
fprintf(stderr, "Press any key to continue...\n");
|
|
|
|
while (getchar() > 0x100);
|
2019-12-03 08:35:49 +11:00
|
|
|
}
|
|
|
|
m.mem[addr] = val;
|
|
|
|
}
|
|
|
|
|
2020-12-06 14:04:22 +11:00
|
|
|
Machine* emul_init(char *binpath, ushort binoffset)
|
2019-12-03 08:35:49 +11:00
|
|
|
{
|
2020-05-24 00:08:40 +10:00
|
|
|
// initialize memory
|
2019-12-03 08:35:49 +11:00
|
|
|
memset(m.mem, 0, 0x10000);
|
2020-12-06 14:04:22 +11:00
|
|
|
FILE *bfp = fopen(binpath, "r");
|
2020-05-24 00:08:40 +10:00
|
|
|
if (!bfp) {
|
2020-12-06 14:04:22 +11:00
|
|
|
fprintf(stderr, "Can't open %s\n", binpath);
|
2020-05-24 00:08:40 +10:00
|
|
|
return NULL;
|
|
|
|
}
|
2020-12-06 14:04:22 +11:00
|
|
|
int i = binoffset;
|
2020-05-24 00:08:40 +10:00
|
|
|
int c = getc(bfp);
|
|
|
|
while (c != EOF) {
|
2020-12-06 14:04:22 +11:00
|
|
|
#ifdef MAX_ROMSIZE
|
|
|
|
if (i >= MAX_ROMSIZE) {
|
|
|
|
fprintf(stderr, "ROM image too large.\n");
|
|
|
|
fclose(fp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
2020-05-24 00:08:40 +10:00
|
|
|
m.mem[i++] = c;
|
|
|
|
c = getc(bfp);
|
|
|
|
}
|
|
|
|
fclose(bfp);
|
2019-12-03 08:35:49 +11:00
|
|
|
m.ramstart = 0;
|
2019-12-03 09:44:54 +11:00
|
|
|
m.minsp = 0xffff;
|
2020-05-15 08:41:09 +10:00
|
|
|
m.maxix = 0;
|
2019-12-03 08:35:49 +11:00
|
|
|
for (int i=0; i<0x100; i++) {
|
|
|
|
m.iord[i] = NULL;
|
|
|
|
m.iowr[i] = NULL;
|
|
|
|
}
|
2020-12-06 14:04:22 +11:00
|
|
|
m.pchooks_cnt = 0;
|
2019-12-03 08:35:49 +11:00
|
|
|
Z80RESET(&m.cpu);
|
|
|
|
m.cpu.memRead = mem_read;
|
|
|
|
m.cpu.memWrite = mem_write;
|
|
|
|
m.cpu.ioRead = io_read;
|
|
|
|
m.cpu.ioWrite = io_write;
|
|
|
|
return &m;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool emul_step()
|
|
|
|
{
|
|
|
|
if (!m.cpu.halted) {
|
|
|
|
Z80Execute(&m.cpu);
|
2019-12-03 09:44:54 +11:00
|
|
|
ushort newsp = m.cpu.R1.wr.SP;
|
|
|
|
if (newsp != 0 && newsp < m.minsp) {
|
|
|
|
m.minsp = newsp;
|
|
|
|
}
|
2020-05-15 08:41:09 +10:00
|
|
|
if (m.cpu.R1.wr.IX > m.maxix) {
|
|
|
|
m.maxix = m.cpu.R1.wr.IX;
|
|
|
|
}
|
2020-12-06 14:04:22 +11:00
|
|
|
for (int i=0; i<m.pchooks_cnt; i++) {
|
|
|
|
if (m.cpu.PC == m.pchooks[i]) {
|
|
|
|
m.pchookfunc(&m);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-12-03 08:35:49 +11:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-02 14:48:01 +11:00
|
|
|
bool emul_steps(unsigned int steps)
|
|
|
|
{
|
|
|
|
while (steps) {
|
|
|
|
if (!emul_step()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
steps--;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-12-03 08:35:49 +11:00
|
|
|
void emul_loop()
|
|
|
|
{
|
|
|
|
while (emul_step());
|
|
|
|
}
|
2019-12-03 09:44:54 +11:00
|
|
|
|
2020-03-30 13:13:54 +11:00
|
|
|
void emul_trace(ushort addr)
|
|
|
|
{
|
|
|
|
ushort newval = m.mem[addr+1] << 8 | m.mem[addr];
|
|
|
|
if (newval != traceval) {
|
|
|
|
traceval = newval;
|
|
|
|
fprintf(stderr, "trace: %04x PC: %04x\n", traceval, m.cpu.PC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-09 22:26:41 +10:00
|
|
|
void emul_memdump()
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Dumping memory to memdump. PC %04x\n", m.cpu.PC);
|
|
|
|
FILE *fp = fopen("memdump", "w");
|
|
|
|
fwrite(m.mem, 0x10000, 1, fp);
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
2020-05-24 04:42:36 +10:00
|
|
|
void emul_debugstr(char *s)
|
|
|
|
{
|
|
|
|
sprintf(s, "SP %04x (%04x) IX %04x (%04x)",
|
|
|
|
m.cpu.R1.wr.SP, m.minsp, m.cpu.R1.wr.IX, m.maxix);
|
|
|
|
}
|
|
|
|
|
2019-12-03 09:44:54 +11:00
|
|
|
void emul_printdebug()
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Min SP: %04x\n", m.minsp);
|
2020-05-15 08:41:09 +10:00
|
|
|
fprintf(stderr, "Max IX: %04x\n", m.maxix);
|
2019-12-03 09:44:54 +11:00
|
|
|
}
|
2020-10-25 02:18:48 +11:00
|
|
|
|
|
|
|
byte iord_noop() { return 0; }
|
|
|
|
void iowr_noop(byte val) {}
|