|
|
1.1 root 1: /* Generator is (c) James Ponder, 1997-2001 http://www.squish.net/generator/ */
2:
3: #include <stdlib.h>
4: #include <stdio.h>
5: #include <string.h>
6:
7: #include "cpuz80.h"
8: #include "cpu68k.h"
9: #include "memz80.h"
10: #include "ui.h"
11:
12: #include "raze.h"
13:
14: /*** variables externed ***/
15:
16: uint8 *cpuz80_ram = NULL;
17: uint32 cpuz80_bank = 0;
1.1.1.3 root 18: uint8 cpuz80_active = 0;
19: uint8 cpuz80_resetting = 0;
1.1 root 20: unsigned int cpuz80_pending = 0;
1.1.1.3 root 21: unsigned int cpuz80_on = 1; /* z80 turned on? */
1.1 root 22:
23: /*** global variables ***/
24:
25: static unsigned int cpuz80_lastsync = 0;
26:
27: int cpuz80_init(void)
28: {
29: cpuz80_reset();
30: return 0;
31: }
32:
33: /*** cpuz80_reset - reset z80 sub-unit ***/
34:
35: void cpuz80_reset(void)
36: {
37: int i;
38:
39: if (!cpuz80_ram) {
40: if ((cpuz80_ram = malloc(LEN_SRAM)) == NULL) {
41: fprintf(stderr, "Out of memory\n");
42: exit(1);
43: }
44: }
45: memset(cpuz80_ram, 0, LEN_SRAM);
46: cpuz80_bank = 0;
47: cpuz80_active = 0;
48: cpuz80_lastsync = 0;
49: cpuz80_resetting = 1;
1.1.1.3 root 50: cpuz80_pending = 0;
1.1 root 51: z80_init_memmap();
52: z80_map_fetch(0x0000, 0x3fff, cpuz80_ram);
1.1.1.2 root 53: z80_map_fetch(0x4000, 0x7fff, cpuz80_ram); /* ok? */
54: z80_map_fetch(0x8000, 0xbfff, cpuz80_ram); /* ok? */
55: z80_map_fetch(0xc000, 0xffff, cpuz80_ram); /* ok? */
56: z80_add_read(0x000, 0x3fff, Z80_MAP_DIRECT, cpuz80_ram);
1.1 root 57: z80_add_write(0x000, 0x3fff, Z80_MAP_DIRECT, cpuz80_ram);
58: for (i = 1; (memz80_def[i].start != 0) || (memz80_def[i].end != 0); i++) {
59: z80_add_read(memz80_def[i].start << 8, (memz80_def[i].end << 8) - 1,
60: Z80_MAP_HANDLED, memz80_def[i].fetch_byte);
61: z80_add_write(memz80_def[i].start << 8, (memz80_def[i].end << 8) - 1,
1.1.1.2 root 62: Z80_MAP_HANDLED, memz80_def[i].store_byte);
1.1 root 63: }
64: z80_end_memmap();
65: z80_reset();
66: }
67:
68: /*** cpuz80_updatecontext - inform z80 processor of changed context ***/
69:
70: void cpuz80_updatecontext(void)
71: {
72: /* not required with raze */
73: }
74:
75: /*** cpuz80_resetcpu - reset z80 cpu ***/
76:
77: void cpuz80_resetcpu(void)
78: {
79: cpuz80_sync();
80: z80_reset();
1.1.1.2 root 81: cpuz80_resetting = 1; /* suspends execution */
1.1 root 82: }
83:
84: /*** cpuz80_unresetcpu - unreset z80 cpu ***/
85:
86: void cpuz80_unresetcpu(void)
87: {
88: if (cpuz80_resetting)
89: cpuz80_sync();
1.1.1.2 root 90: cpuz80_resetting = 0; /* un-suspends execution */
1.1 root 91: }
92:
93: /*** cpuz80_bankwrite - data is being written to latch ***/
94:
95: void cpuz80_bankwrite(uint8 data)
96: {
1.1.1.2 root 97: cpuz80_bank = (((cpuz80_bank >> 1) | ((data & 1) << 23)) & 0xff8000);
1.1 root 98: }
99:
100: /*** cpuz80_stop - stop the processor ***/
101:
102: void cpuz80_stop(void)
103: {
104: cpuz80_sync();
105: cpuz80_active = 0;
106: }
107:
108: /*** cpuz80_start - start the processor ***/
109:
110: void cpuz80_start(void)
111: {
112: if (!cpuz80_active)
113: cpuz80_sync();
114: cpuz80_active = 1;
115: }
116:
117: /*** cpuz80_endfield - reset counters ***/
118:
119: void cpuz80_endfield(void)
120: {
121: cpuz80_lastsync = 0;
122: }
123:
124: /*** cpuz80_sync - synchronise ***/
125:
126: void cpuz80_sync(void)
127: {
128: int cpu68k_wanted = cpu68k_clocks - cpuz80_lastsync;
1.1.1.2 root 129: int wanted = (cpu68k_wanted < 0 ? 0 : cpu68k_wanted) * 7 / 15;
1.1.1.4 ! root 130: int achieved;
1.1 root 131:
132: if (cpuz80_pending && z80_get_reg(Z80_REG_IFF1)) {
133: z80_raise_IRQ(0xff);
134: z80_lower_IRQ();
135: cpuz80_pending = 0;
136: }
1.1.1.3 root 137: if (cpuz80_on && cpuz80_active && !cpuz80_resetting) {
1.1 root 138: /* ui_log(LOG_USER, "executing %d z80 clocks @ %X", wanted,
139: cpuz80_z80.z80pc); */
1.1.1.4 ! root 140: achieved = z80_emulate(wanted);
! 141: cpuz80_lastsync = cpuz80_lastsync + achieved * 15 / 7;
1.1 root 142: } else {
143: cpuz80_lastsync = cpu68k_clocks;
144: }
145: }
146:
147: /*** cpuz80_interrupt - cause an interrupt on the z80 */
148:
149: void cpuz80_interrupt(void)
150: {
151: if (!cpuz80_resetting) {
152: if (z80_get_reg(Z80_REG_IFF1) == 0)
153: cpuz80_pending = 1;
154: z80_raise_IRQ(0xff);
155: z80_lower_IRQ();
156: }
157: }
158:
159: void cpuz80_uninterrupt(void)
160: {
161: z80_lower_IRQ();
162: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.