-
Notifications
You must be signed in to change notification settings - Fork 1
/
z80.c
182 lines (159 loc) · 3.07 KB
/
z80.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* $Id$ */
#include <kos.h>
#include "gen-emu.h"
#include "cart.h"
#include "vdp.h"
#include "SN76489.h"
#include "z80.h"
#include "m68k.h"
UINT8 z80_read_port(UINT32 addr);
void z80_write_port(UINT32 addr, UINT8 val);
UINT8 z80_read_mem(UINT32 addr);
void z80_write_mem(UINT32 addr, UINT8 val);
uint8_t z80_ram[0x2000]; /* 8KB */
uint32_t z80_bank_base;
uint8_t z80_bank_shift;
uint8_t z80_running;
uint8_t z80_busreq;
void z80_dump_mem(void)
{
int fd;
fd = fs_open("/pc/home/jkf/src/dc/gen-emu/z80ram.bin", O_WRONLY | O_TRUNC);
fs_write(fd, z80_ram, 8192);
fs_close(fd);
fd = fs_open("/pc/home/jkf/src/dc/gen-emu/dcvram.bin", O_WRONLY | O_TRUNC);
fs_write(fd, (char *)0x85000000, 8*1024*1024);
fs_close(fd);
}
uint32_t z80init(void)
{
z80_init();
z80_reset(NULL);
return 0;
}
UINT8 z80_read_port(UINT32 addr)
{
return 0xff;
}
void z80_write_port(UINT32 addr, UINT8 val)
{
}
UINT8 z80_read_mem(UINT32 addr)
{
UINT8 ret = 0xff;
addr &= 0xffff;
if (addr < 0x4000) {
ret = z80_ram[addr & 0x1fff];
} else
if (addr >= 0x8000) {
ret = m68k_read_memory_8(addr & 0x7fff);
} else
if ((addr >= 0x7f00) && (addr < 0x7f20)) {
/* vdp */
switch(addr & 0x1f) {
case 0x00:
case 0x02:
ret = (vdp_data_read() >> 8);
break;
case 0x01:
case 0x03:
ret = (vdp_data_read() & 0xff);
break;
case 0x04:
case 0x06:
ret = (vdp_control_read() >> 8);
break;
case 0x05:
case 0x07:
ret = (vdp_control_read() & 0xff);
break;
case 0x08:
case 0x0a:
case 0x0c:
case 0x0e:
printf("hv counter read.\n");
ret = (vdp_hv_read() >> 8);
break;
case 0x09:
case 0x0b:
case 0x0d:
case 0x0f:
printf("hv counter read.\n");
ret = (vdp_hv_read() & 0xff);
break;
}
} else
if ((addr >= 0x4000) && (addr < 0x6000)) {
switch(addr & 0x3) {
case 0x0:
ret = 0x00;
break;
}
}
if (debug)
printf("Z80 %04x -> %02x\n", addr, ret);
return(ret);
}
void z80_write_mem(UINT32 addr, UINT8 val)
{
addr &= 0xffff;
// if ((addr == 0) && (val == 2)) {
// z80_dump_mem();
// asm("trapa #0x20");
// }
if (debug)
printf("Z80 %04x <- %02x\n", addr, val);
if (addr < 0x4000) {
z80_ram[addr & 0x1fff] = val;
} else
if (addr >= 0x8000) {
m68k_write_memory_8(addr & 0x7fff, val);
} else
if ((addr >= 0x7f00) && (addr <= 0x7f1f)) {
/* vdp */
switch(addr & 0x1f) {
case 0x00:
case 0x01:
case 0x02:
case 0x03:
vdp_data_write((val << 8) | val);
break;
case 0x04:
case 0x05:
case 0x06:
case 0x07:
vdp_control_write((val << 8) | val);
break;
case 0x11:
case 0x13:
case 0x15:
case 0x17:
// Write76489(&PSG,val);
break;
}
} else
if ((addr >= 0x4000) && (addr < 0x6000)) {
switch(addr & 0x3) {
case 0x0:
break;
case 0x1:
break;
case 0x2:
break;
case 0x3:
break;
}
}
if ((addr >= 0x6000) && (addr <= 0x60ff)) {
static uint32_t tmp_bank_base;
tmp_bank_base |= ((val & 0x01) << z80_bank_shift++);
if (z80_bank_shift == 9) {
z80_bank_base = tmp_bank_base << 15;
z80_bank_shift = 0;
}
}
}
uint32_t z80_enabled(void)
{
return (z80_running && !z80_busreq);
}