]>
Commit | Line | Data |
---|---|---|
35edb766 L |
1 | /* |
2 | * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de> | |
3 | * | |
4 | * SPDX-License-Identifier: GPL-2.0+ | |
5 | */ | |
6 | ||
72f58822 | 7 | #include "common.h" |
89adce76 | 8 | #include <util/atomic.h> |
72f58822 | 9 | |
89adce76 | 10 | #include "background.h" |
72f58822 L |
11 | #include "serial.h" |
12 | #include "z80-if.h" | |
889202c4 | 13 | #include "debug.h" |
89adce76 | 14 | #include "z180-serv.h" |
72f58822 | 15 | |
72f58822 L |
16 | /*--------------------------------------------------------------------------*/ |
17 | ||
72f58822 L |
18 | |
19 | uint8_t z80_get_byte(uint32_t adr) | |
20 | { | |
21 | uint8_t data; | |
8a7decea | 22 | |
62f624d3 | 23 | z80_bus_cmd(Request); |
89adce76 | 24 | data = z80_read(adr); |
62f624d3 | 25 | z80_bus_cmd(Release); |
8a7decea | 26 | |
72f58822 L |
27 | return data; |
28 | } | |
29 | ||
30 | ||
31 | /*--------------------------------------------------------------------------*/ | |
32 | ||
33 | struct msg_item { | |
34 | uint8_t fct; | |
35 | uint8_t sub_min, sub_max; | |
36 | void (*func)(uint8_t, int, uint8_t *); | |
37 | }; | |
38 | ||
39 | uint32_t msg_to_addr(uint8_t *msg) | |
40 | { | |
41 | union { | |
42 | uint32_t as32; | |
43 | uint8_t as8[4]; | |
44 | } addr; | |
45 | ||
46 | addr.as8[0] = msg[0]; | |
47 | addr.as8[1] = msg[1]; | |
48 | addr.as8[2] = msg[2]; | |
49 | addr.as8[3] = 0; | |
50 | ||
51 | return addr.as32; | |
52 | } | |
53 | ||
72f58822 | 54 | |
1a2460dc L |
55 | static int msg_xmit_header(uint8_t func, uint8_t subf, int len) |
56 | { | |
57 | z80_memfifo_putc(fifo_msgout, 0xAE); | |
58 | z80_memfifo_putc(fifo_msgout, len+2); | |
59 | z80_memfifo_putc(fifo_msgout, func); | |
60 | z80_memfifo_putc(fifo_msgout, subf); | |
61 | ||
62 | return 0; | |
63 | } | |
64 | ||
65 | int msg_xmit(uint8_t func, uint8_t subf, int len, uint8_t *msg) | |
66 | { | |
67 | msg_xmit_header(func, subf, len); | |
68 | while (len--) | |
69 | z80_memfifo_putc(fifo_msgout, *msg++); | |
70 | ||
71 | return 0; | |
72 | } | |
73 | ||
72f58822 L |
74 | void do_msg_ini_memfifo(uint8_t subf, int len, uint8_t * msg) |
75 | { | |
76 | (void)len; | |
77 | ||
89adce76 | 78 | z80_memfifo_init(subf, msg_to_addr(msg)); |
72f58822 L |
79 | } |
80 | ||
81 | ||
82 | void do_msg_char_out(uint8_t subf, int len, uint8_t * msg) | |
83 | { | |
84 | (void)subf; | |
85 | ||
86 | while (len--) | |
87 | putchar(*msg++); | |
88 | } | |
89 | ||
1a2460dc L |
90 | /* echo message */ |
91 | void do_msg_echo(uint8_t subf, int len, uint8_t * msg) | |
92 | { | |
93 | (void)subf; | |
94 | ||
95 | /* send re-echo */ | |
96 | msg_xmit(1, 3, len, msg); | |
97 | } | |
98 | ||
72f58822 L |
99 | |
100 | const FLASH struct msg_item z80_messages[] = | |
101 | { | |
102 | { 0, /* fct nr. */ | |
89adce76 | 103 | 1, 3, /* sub fct nr. from, to */ |
72f58822 L |
104 | do_msg_ini_memfifo}, |
105 | { 1, | |
106 | 1, 1, | |
107 | do_msg_char_out}, | |
1a2460dc L |
108 | { 1, |
109 | 2, 2, | |
110 | do_msg_echo}, | |
72f58822 L |
111 | { 0xff, /* end mark */ |
112 | 0, 0, | |
113 | 0}, | |
114 | ||
115 | }; | |
116 | ||
117 | ||
118 | ||
119 | ||
120 | void do_message(int len, uint8_t *msg) | |
121 | { | |
122 | uint8_t fct, sub_fct; | |
123 | int_fast8_t i = 0; | |
124 | ||
125 | if (len >= 2) { | |
126 | fct = *msg++; | |
127 | sub_fct = *msg++; | |
128 | len -= 2; | |
129 | ||
89adce76 L |
130 | while (fct != z80_messages[i].fct) { |
131 | if (z80_messages[i].fct == 0xff) { | |
132 | DBG_P(1, "do_message: Unknown function: %i, %i\n", | |
133 | fct, sub_fct); | |
134 | return; /* TODO: unknown message # */ | |
135 | } | |
8a7decea | 136 | |
72f58822 | 137 | ++i; |
72f58822 L |
138 | } |
139 | ||
140 | while (fct == z80_messages[i].fct) { | |
8a7decea | 141 | if (sub_fct >= z80_messages[i].sub_min && |
89adce76 | 142 | sub_fct <= z80_messages[i].sub_max ) |
72f58822 L |
143 | break; |
144 | ++i; | |
145 | } | |
146 | ||
147 | if (z80_messages[i].fct != fct) { | |
148 | DBG_P(1, "do_message: Unknown sub function: %i, %i\n", | |
149 | fct, sub_fct); | |
150 | return; /* TODO: unknown message sub# */ | |
151 | } | |
152 | ||
153 | (z80_messages[i].func)(sub_fct, len, msg); | |
154 | ||
155 | ||
156 | } else { | |
157 | /* TODO: error */ | |
158 | DBG_P(1, "do_message: to few arguments (%i); this shouldn't happen!\n", len); | |
159 | } | |
160 | } | |
161 | ||
162 | ||
163 | ||
164 | #define CTRBUF_LEN 256 | |
165 | ||
166 | void check_msg_fifo(void) | |
167 | { | |
168 | int ch; | |
169 | static int_fast8_t state; | |
170 | static int msglen,idx; | |
171 | static uint8_t buffer[CTRBUF_LEN]; | |
172 | ||
89adce76 | 173 | while ((ch = z80_memfifo_getc(fifo_msgin)) >= 0) { |
72f58822 L |
174 | switch (state) { |
175 | case 0: /* wait for start of message */ | |
3531528e | 176 | if (ch == 0xAE) { /* TODO: magic number */ |
72f58822 L |
177 | msglen = 0; |
178 | idx = 0; | |
179 | state = 1; | |
180 | } | |
181 | break; | |
182 | case 1: /* get msg len */ | |
183 | if (ch > 0 && ch <= CTRBUF_LEN) { | |
184 | msglen = ch; | |
185 | state = 2; | |
186 | } else | |
187 | state = 0; | |
188 | break; | |
189 | case 2: /* get message */ | |
190 | buffer[idx++] = ch; | |
89adce76 L |
191 | if (idx == msglen) { |
192 | do_message(msglen, buffer); | |
193 | state = 0; | |
194 | } | |
195 | break; | |
196 | } | |
197 | } | |
198 | } | |
199 | ||
200 | ||
201 | int msg_handling(int state) | |
202 | { | |
203 | uint8_t pending; | |
8a7decea L |
204 | |
205 | ATOMIC_BLOCK(ATOMIC_FORCEON) { | |
89adce76 L |
206 | pending = (Stat & S_MSG_PENDING) != 0; |
207 | Stat &= ~S_MSG_PENDING; | |
208 | } | |
8a7decea | 209 | |
89adce76 L |
210 | if (pending) { |
211 | switch (state) { | |
1a2460dc | 212 | case 0: /* need init */ |
89adce76 L |
213 | z80_bus_cmd(Request); |
214 | uint32_t addr = z80_read(0x40) + | |
215 | ((uint16_t) z80_read(0x41) << 8) + | |
216 | ((uint32_t) z80_read(0x42) << 16); | |
217 | z80_bus_cmd(Release); | |
218 | if (addr != 0) { | |
219 | z80_memfifo_init(fifo_msgin, addr); | |
220 | state = 1; | |
221 | } | |
222 | break; | |
1a2460dc | 223 | case 1: /* awaiting messages */ |
89adce76 | 224 | check_msg_fifo(); |
72f58822 L |
225 | break; |
226 | } | |
227 | } | |
228 | ||
89adce76 L |
229 | return state; |
230 | } | |
89adce76 L |
231 | |
232 | ||
233 | static int handle_msg_handling; | |
234 | ||
235 | void setup_z180_serv(void) | |
236 | { | |
8a7decea | 237 | |
89adce76 | 238 | handle_msg_handling = bg_register(msg_handling, 0); |
72f58822 L |
239 | } |
240 | ||
89adce76 L |
241 | void restart_z180_serv(void) |
242 | { | |
243 | z80_bus_cmd(Request); | |
244 | z80_write(0x40, 0); | |
245 | z80_write(0x41, 0); | |
246 | z80_write(0x42, 0); | |
247 | z80_bus_cmd(Release); | |
8a7decea L |
248 | |
249 | for (int i = 0; i < NUM_FIFOS; i++) | |
250 | z80_memfifo_init(i, 0); | |
89adce76 L |
251 | bg_setstat(handle_msg_handling, 0); |
252 | } | |
72f58822 L |
253 | |
254 | /*--------------------------------------------------------------------------*/ | |
255 | ||
72f58822 L |
256 | const FLASH uint8_t iniprog[] = { |
257 | 0xAF, // xor a | |
258 | 0xED, 0x39, 0x36, // out0 (rcr),a ;disable DRAM refresh | |
259 | 0x3E, 0x30, // ld a,030h | |
260 | 0xED, 0x39, 0x32 //out0 (dcntl),a ;0 mem, max i/0 wait states | |
261 | }; | |
262 | ||
263 | const FLASH uint8_t sertest[] = { | |
264 | 0xAF, // xor a | |
265 | 0xED, 0x39, 0x36, // out0 (rcr),a ;disable DRAM refresh | |
266 | 0x3E, 0x30, // ld a,030h | |
267 | 0xED, 0x39, 0x32, // out0 (dcntl),a ;0 mem, max i/0 wait states | |
268 | 0x3E, 0x80, // ld a,M_MPBT ;no MP, PS=10, DR=16, SS=0 | |
269 | 0xED, 0x39, 0x03, // out0 (cntlb1),a | |
270 | 0x3E, 0x64, // ld a,M_RE + M_TE + M_MOD2 ; | |
271 | 0xED, 0x39, 0x01, // out0 (cntla1),a | |
272 | 0x3E, 0x00, // ld a,0 | |
273 | 0xED, 0x39, 0x05, // out0 (stat1),a ;Enable rx interrupts | |
274 | 0xED, 0x38, 0x05, //l0:in0 a,(stat1) | |
275 | 0xE6, 0x80, // and 80h | |
276 | 0x28, 0xF9, // jr z,l0 | |
277 | 0xED, 0x00, 0x09, // in0 b,(rdr1) | |
278 | 0xED, 0x38, 0x05, //l1:in0 a,(stat1) | |
279 | 0xE6, 0x02, // and 02h | |
280 | 0x28, 0xF9, // jr z,l1 | |
281 | 0xED, 0x01, 0x07, // out0 (tdr1),b | |
282 | 0x18, 0xEA, // jr l0 | |
283 | }; | |
284 | ||
285 | const FLASH uint8_t test1[] = { | |
286 | 0xAF, // xor a | |
287 | 0xED, 0x39, 0x36, // out0 (rcr),a ;disable DRAM refresh | |
288 | 0x3E, 0x30, // ld a,030h | |
289 | 0xED, 0x39, 0x32, // out0 (dcntl),a ;0 mem, max i/0 wait states | |
290 | 0x21, 0x1E, 0x00, // ld hl,dmclrt ;load DMA registers | |
291 | 0x06, 0x08, // ld b,dmct_e-dmclrt | |
292 | 0x0E, 0x20, // ld c,sar0l | |
8a7decea | 293 | 0xED, 0x93, // otimr |
72f58822 L |
294 | 0x3E, 0xC3, // ld a,0c3h ;dst +1, src +1, burst |
295 | 0xED, 0x39, 0x31, // out0 (dmode),a ; | |
8a7decea | 296 | 0x3E, 0x62, // ld a,062h ;enable dma0, |
72f58822 L |
297 | 0xED, 0x39, 0x30, //cl_1: out0 (dstat),a ;copy 64k |
298 | 0x18, 0xFB, // jr cl_1 ; | |
8a7decea | 299 | 0x00, 0x00, //dmclrt: dw 0 ;src (inc) |
72f58822 L |
300 | 0x00, // db 0 ;src |
301 | 0x00, 0x00, // dw 0 ;dst (inc), | |
302 | 0x00, // db 0 ;dst | |
303 | 0x00, 0x00, // dw 0 ;count (64k) | |
304 | }; |