]>
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" |
5f7f3586 L |
8 | #include <stdlib.h> |
9 | #include <string.h> | |
10 | #include <stdbool.h> | |
89adce76 | 11 | #include <util/atomic.h> |
72f58822 | 12 | |
89adce76 | 13 | #include "background.h" |
5f7f3586 L |
14 | #include "env.h" |
15 | #include "ff.h" | |
72f58822 L |
16 | #include "serial.h" |
17 | #include "z80-if.h" | |
889202c4 | 18 | #include "debug.h" |
5f7f3586 | 19 | #include "print-utils.h" |
89adce76 | 20 | #include "z180-serv.h" |
daacc6f9 | 21 | #include "timer.h" |
845cbdbd L |
22 | #include "time.h" |
23 | #include "bcd.h" | |
24 | #include "rtc.h" | |
7d60b20b L |
25 | |
26 | #define DEBUG_CPM_SDIO 0 /* set to 1 to debug */ | |
27 | ||
28 | #define debug_cpmsd(fmt, args...) \ | |
29 | debug_cond(DEBUG_CPM_SDIO, fmt, ##args) | |
30 | ||
31 | ||
72f58822 L |
32 | /*--------------------------------------------------------------------------*/ |
33 | ||
34 | struct msg_item { | |
35 | uint8_t fct; | |
36 | uint8_t sub_min, sub_max; | |
37 | void (*func)(uint8_t, int, uint8_t *); | |
38 | }; | |
39 | ||
40 | uint32_t msg_to_addr(uint8_t *msg) | |
41 | { | |
42 | union { | |
43 | uint32_t as32; | |
44 | uint8_t as8[4]; | |
45 | } addr; | |
46 | ||
47 | addr.as8[0] = msg[0]; | |
48 | addr.as8[1] = msg[1]; | |
49 | addr.as8[2] = msg[2]; | |
50 | addr.as8[3] = 0; | |
51 | ||
52 | return addr.as32; | |
53 | } | |
54 | ||
72f58822 | 55 | |
1a2460dc L |
56 | static int msg_xmit_header(uint8_t func, uint8_t subf, int len) |
57 | { | |
58 | z80_memfifo_putc(fifo_msgout, 0xAE); | |
59 | z80_memfifo_putc(fifo_msgout, len+2); | |
60 | z80_memfifo_putc(fifo_msgout, func); | |
61 | z80_memfifo_putc(fifo_msgout, subf); | |
62 | ||
63 | return 0; | |
64 | } | |
65 | ||
66 | int msg_xmit(uint8_t func, uint8_t subf, int len, uint8_t *msg) | |
67 | { | |
68 | msg_xmit_header(func, subf, len); | |
69 | while (len--) | |
70 | z80_memfifo_putc(fifo_msgout, *msg++); | |
71 | ||
72 | return 0; | |
73 | } | |
74 | ||
72f58822 L |
75 | void do_msg_ini_memfifo(uint8_t subf, int len, uint8_t * msg) |
76 | { | |
77 | (void)len; | |
78 | ||
89adce76 | 79 | z80_memfifo_init(subf, msg_to_addr(msg)); |
72f58822 L |
80 | } |
81 | ||
82 | ||
83 | void do_msg_char_out(uint8_t subf, int len, uint8_t * msg) | |
84 | { | |
85 | (void)subf; | |
86 | ||
87 | while (len--) | |
88 | putchar(*msg++); | |
89 | } | |
90 | ||
1a2460dc L |
91 | /* echo message */ |
92 | void do_msg_echo(uint8_t subf, int len, uint8_t * msg) | |
93 | { | |
94 | (void)subf; | |
95 | ||
96 | /* send re-echo */ | |
97 | msg_xmit(1, 3, len, msg); | |
98 | } | |
99 | ||
8590a76b L |
100 | /* get timer */ |
101 | void do_msg_get_timer(uint8_t subf, int len, uint8_t * msg) | |
102 | { | |
a8eb521f | 103 | uint32_t time_ms = (len >= 4) ? *(uint32_t *) msg : 0; |
8590a76b | 104 | |
a8eb521f | 105 | time_ms = get_timer(time_ms); |
8590a76b L |
106 | msg_xmit(3, subf, sizeof(time_ms), (uint8_t *) &time_ms); |
107 | } | |
108 | ||
5f7f3586 L |
109 | /* ---------------------------------------------------------------------------*/ |
110 | ||
845cbdbd L |
111 | #define CPM_DAY_OFFSET ((1978-1900) * 365 + 19) /* 19 leap years */ |
112 | ||
113 | /* | |
114 | * Convert CP/M time stamp to a broken-down time structure | |
115 | * | |
116 | */ | |
117 | int mk_date_time (int len, uint8_t *msg, struct tm *tmp) | |
118 | { | |
119 | time_t stamp; | |
120 | ||
121 | if (len != 5) | |
122 | return -1; | |
123 | ||
124 | /* days since 2000-01-01 */ | |
125 | long days = msg[3] + (msg[4] << 8) - 8036; | |
126 | ||
127 | if (days < 0) | |
128 | return -1; | |
129 | ||
130 | stamp = days * ONE_DAY; | |
131 | stamp += bcd2bin(msg[0]); | |
132 | stamp += bcd2bin(msg[1]) * 60 ; | |
8d0fad4c | 133 | stamp += bcd2bin(msg[2]) * 3600L; |
845cbdbd L |
134 | gmtime_r(&stamp, tmp); |
135 | return 0; | |
136 | } | |
137 | ||
138 | void mk_cpm_time(struct tm *tmp, uint8_t cpm_time[5]) | |
139 | { | |
140 | uint16_t days = 1; | |
141 | uint_fast8_t leap=2; | |
142 | ||
143 | for (int year=78; year < tmp->tm_year; year++) { | |
144 | days = days + 365 + (leap == 0); | |
145 | leap = (leap+1)%4; | |
146 | } | |
147 | days += tmp->tm_yday; | |
148 | ||
149 | cpm_time[0] = bin2bcd(tmp->tm_sec); | |
150 | cpm_time[1] = bin2bcd(tmp->tm_min); | |
151 | cpm_time[2] = bin2bcd(tmp->tm_hour); | |
152 | cpm_time[3] = days; | |
153 | cpm_time[4] = days >> 8; | |
845cbdbd L |
154 | } |
155 | ||
156 | /* get/set cp/m time */ | |
157 | void do_msg_get_set_time(uint8_t subf, int len, uint8_t * msg) | |
158 | { | |
159 | struct tm t; | |
160 | uint8_t cpm_time[5]; | |
161 | int rc; | |
162 | ||
163 | memset(cpm_time, 0, ARRAY_SIZE(cpm_time)); | |
164 | ||
165 | switch (subf) { | |
166 | case 3: /* set date & time */ | |
167 | /* initialize t with current time */ | |
168 | rc = rtc_get (&t); | |
169 | ||
170 | if (rc == 0) { | |
171 | /* insert new date & time */ | |
172 | if (mk_date_time (len, msg, &t) != 0) { | |
173 | my_puts_P(PSTR("## set_time: Bad date format\n")); | |
174 | break; | |
175 | } | |
176 | ||
177 | time_t time; | |
178 | time = mk_gmtime(&t); | |
179 | gmtime_r(&time, &t); | |
180 | ||
181 | /* and write to RTC */ | |
182 | rc = rtc_set (&t); | |
183 | if(rc) | |
184 | my_puts_P(PSTR("## set_time: Set date failed\n")); | |
185 | } else { | |
186 | my_puts_P(PSTR("## set_time: Get date failed\n")); | |
187 | } | |
188 | /* FALL TROUGH */ | |
189 | case 2: /* get date & time */ | |
190 | rc = rtc_get (&t); | |
191 | ||
192 | if (rc) { | |
193 | my_puts_P(PSTR("## get_time: Get date failed\n")); | |
194 | break; | |
195 | } | |
196 | ||
197 | time_t time; | |
198 | time = mk_gmtime(&t); | |
199 | //mktime(&t); | |
200 | gmtime_r(&time, &t); | |
201 | ||
202 | ||
203 | mk_cpm_time(&t, cpm_time); | |
204 | break; | |
205 | } | |
206 | ||
207 | msg_xmit(3, subf, sizeof(cpm_time), cpm_time); | |
208 | } | |
209 | ||
210 | /* ---------------------------------------------------------------------------*/ | |
211 | ||
01484095 | 212 | #define MAX_DRIVE 4 |
5f7f3586 | 213 | #define BLOCK_SIZE 512 |
8bbf185e L |
214 | //#define TPA_BASE 0x10000 |
215 | //#define COMMON_BASE 0xC000 | |
5f7f3586 L |
216 | |
217 | struct cpm_drive_s { | |
218 | uint8_t drv; | |
219 | uint8_t device; | |
220 | char *img_name; | |
393b1897 | 221 | bool dirty; |
5f7f3586 L |
222 | FIL fd; |
223 | }; | |
224 | ||
225 | static uint8_t disk_buffer[BLOCK_SIZE]; | |
5f7f3586 | 226 | static struct cpm_drive_s drv_table[MAX_DRIVE]; |
393b1897 L |
227 | static int handle_cpm_drv_to; |
228 | ||
229 | #define f_dirty(fp) ((fp)->fs->wflag != 0) | |
230 | ||
231 | ||
232 | int cpm_drv_to(int state) | |
233 | { | |
234 | static uint32_t ts; | |
235 | ||
236 | switch(state) { | |
237 | case 0: | |
238 | break; | |
239 | ||
240 | case 1: | |
241 | ts = get_timer(0); | |
242 | state = 2; | |
243 | break; | |
244 | ||
245 | case 2: | |
246 | if (get_timer(ts) > 1000) { | |
247 | for (uint_fast8_t i=0; i < MAX_DRIVE; i++) { | |
248 | // if (&drv_table[i].fd && f_dirty(&drv_table[i].fd)) { | |
249 | if (drv_table[i].dirty) { | |
250 | f_sync(&drv_table[i].fd); | |
251 | drv_table[i].dirty = false; | |
252 | debug_cpmsd("## %7lu f_sync: %c:\n", get_timer(0), i+'A'); | |
253 | } | |
254 | } | |
255 | state = 0; | |
256 | } | |
257 | } | |
258 | return state; | |
259 | } | |
260 | ||
261 | ||
262 | void msg_cpm_result(uint8_t subf, uint8_t rc, int res) | |
263 | { | |
264 | uint8_t result_msg[3]; | |
265 | ||
266 | if (res) | |
267 | rc |= 0x80; | |
268 | ||
269 | result_msg[0] = rc; | |
270 | result_msg[1] = res; | |
271 | result_msg[2] = res >> 8; | |
272 | ||
273 | if (rc) { | |
274 | debug_cpmsd("###%7lu error rc: %.02x, res: %d\n", get_timer(0), rc, res); | |
275 | } | |
276 | ||
277 | msg_xmit(2, subf, sizeof(result_msg), result_msg); | |
278 | } | |
5f7f3586 L |
279 | |
280 | /* | |
281 | db 2 ; disk command | |
282 | ds 1 ; subcommand (login/read/write) | |
283 | ds 1 ; @adrv (8 bits) +0 | |
284 | ds 1 ; @rdrv (8 bits) +1 | |
285 | ds 3 ; @xdph (24 bits) +2 | |
286 | */ | |
287 | ||
288 | void do_msg_cpm_login(uint8_t subf, int len, uint8_t * msg) | |
289 | { | |
290 | ||
291 | FRESULT res = 0; | |
5f7f3586 L |
292 | uint8_t drv; |
293 | char *np; | |
5f7f3586 L |
294 | |
295 | (void)subf; | |
296 | ||
297 | if (len != 5) { /* TODO: check adrv, rdrv */ | |
393b1897 | 298 | return msg_cpm_result(subf, 0x01, res); |
5f7f3586 L |
299 | } |
300 | ||
7d60b20b | 301 | debug_cpmsd("\n## %7lu login: %c:\n", get_timer(0), msg[0]+'A'); |
5f7f3586 L |
302 | |
303 | ||
304 | drv = msg[0]; | |
305 | if ( drv>= MAX_DRIVE) { | |
393b1897 | 306 | return msg_cpm_result(subf, 0x02, res); |
5f7f3586 L |
307 | } |
308 | ||
5f7f3586 L |
309 | /* |
310 | uint32_t dph = ((uint32_t)msg[4] << 16) + ((uint16_t)msg[3] << 8) + msg[2]; | |
311 | */ | |
312 | ||
313 | if (drv_table[drv].img_name != NULL) { | |
7d60b20b | 314 | debug_cpmsd("## %7lu close: '%s'\n", get_timer(0), drv_table[drv].img_name); |
5f7f3586 | 315 | f_close(&drv_table[drv].fd); |
393b1897 | 316 | drv_table[drv].dirty = false; |
5f7f3586 L |
317 | free(drv_table[drv].img_name); |
318 | drv_table[drv].img_name = NULL; | |
319 | } | |
320 | ||
321 | strcpy_P((char *)disk_buffer, PSTR("dsk0")); | |
322 | disk_buffer[3] = msg[0] + '0'; | |
323 | if (((np = getenv((char*)disk_buffer)) == NULL) || | |
324 | ((drv_table[drv].img_name = strdup(np)) == NULL)) { | |
393b1897 | 325 | return msg_cpm_result(subf, 0x03, res); |
5f7f3586 L |
326 | } |
327 | ||
328 | ||
329 | res = f_open(&drv_table[drv].fd, drv_table[drv].img_name, | |
330 | FA_WRITE | FA_READ); | |
331 | ||
7d60b20b | 332 | debug_cpmsd("## %7lu open: '%s', (env: '%s'), res: %d\n", get_timer(0), |
5f7f3586 L |
333 | drv_table[drv].img_name, disk_buffer, res); |
334 | ||
5f7f3586 | 335 | /* send result*/ |
393b1897 | 336 | msg_cpm_result(subf, 0x00, res); |
5f7f3586 L |
337 | } |
338 | ||
339 | ||
340 | /* | |
341 | db 2 ; disk command | |
342 | ds 1 ; subcommand (login/read/write) | |
daacc6f9 L |
343 | ds 1 ; @adrv (8 bits) +0 |
344 | ds 1 ; @rdrv (8 bits) +1 | |
345 | ds 2 ; @trk (16 bits) +2 | |
346 | ds 2 ; @sect(16 bits) +4 | |
347 | ds 1 ; @cnt (8 bits) +6 | |
5f7f3586 L |
348 | ds 3 ; phys. transfer addr +7 |
349 | */ | |
350 | ||
daacc6f9 L |
351 | #define ADRV 0 |
352 | #define RDRV 1 | |
353 | #define TRK 2 | |
354 | #define SEC 4 | |
355 | #define CNT 6 | |
356 | #define ADDR 7 | |
357 | ||
5f7f3586 L |
358 | void do_msg_cpm_rw(uint8_t subf, int len, uint8_t * msg) |
359 | { | |
360 | uint8_t drv; | |
361 | uint32_t addr; | |
362 | uint32_t pos; | |
7d60b20b | 363 | uint8_t secs; |
5f7f3586 L |
364 | bool dowrite = (subf == 2); |
365 | FRESULT res = 0; | |
366 | uint8_t rc = 0; | |
367 | bool buserr = 0; | |
5f7f3586 L |
368 | |
369 | if (len != 10) { /* TODO: check adrv, rdrv */ | |
393b1897 | 370 | return msg_cpm_result(subf, 0x01, res); |
5f7f3586 L |
371 | } |
372 | ||
daacc6f9 | 373 | drv = msg[ADRV]; |
5f7f3586 | 374 | if ( drv>= MAX_DRIVE) { |
393b1897 | 375 | return msg_cpm_result(subf, 0x02, res); |
5f7f3586 L |
376 | } |
377 | ||
7d60b20b | 378 | secs = msg[CNT]; |
daacc6f9 | 379 | addr = ((uint32_t)msg[ADDR+2] << 16) + ((uint16_t)msg[ADDR+1] << 8) + msg[ADDR]; |
5f7f3586 | 380 | |
5f7f3586 | 381 | |
daacc6f9 L |
382 | /* TODO: tracks per sector from dpb */ |
383 | pos = (((uint16_t)(msg[TRK+1] << 8) + msg[TRK]) * 8 | |
384 | + ((uint32_t)(msg[SEC+1] << 8) + msg[SEC])) * BLOCK_SIZE; | |
5f7f3586 | 385 | |
7d60b20b L |
386 | debug_cpmsd("## %7lu cpm_rw: %s %c: trk:%4d, sec: %d, pos: %.8lx, secs: %2d, " |
387 | "addr: %.5lx\n", get_timer(0), dowrite ? "write" : " read", | |
388 | msg[ADRV]+'A', ((uint16_t)(msg[TRK+1] << 8) + msg[TRK]), msg[SEC], | |
389 | pos, msg[CNT], addr); | |
5f7f3586 L |
390 | |
391 | res = f_lseek(&drv_table[drv].fd, pos); | |
7d60b20b | 392 | while (!res && secs--) { |
8bbf185e | 393 | unsigned int brw; |
5f7f3586 L |
394 | if (dowrite) { |
395 | if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) { | |
396 | buserr = 1; | |
7d60b20b | 397 | break; |
5f7f3586 | 398 | } else { |
8bbf185e | 399 | z80_read_block(disk_buffer, addr, BLOCK_SIZE); |
5f7f3586 | 400 | z80_bus_cmd(Release); |
5f7f3586 | 401 | } |
8bbf185e | 402 | res = f_write(&drv_table[drv].fd, disk_buffer, BLOCK_SIZE, &brw); |
5f7f3586 | 403 | } else { |
8bbf185e L |
404 | res = f_read(&drv_table[drv].fd, disk_buffer, BLOCK_SIZE, &brw); |
405 | if (res == FR_OK && brw == BLOCK_SIZE) { | |
5f7f3586 L |
406 | if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) { |
407 | buserr = 1; | |
7d60b20b | 408 | break; |
5f7f3586 | 409 | } else { |
8bbf185e | 410 | z80_write_block(disk_buffer, addr, BLOCK_SIZE); |
5f7f3586 L |
411 | z80_bus_cmd(Release); |
412 | } | |
413 | } | |
414 | } | |
415 | ||
8bbf185e L |
416 | if (brw != BLOCK_SIZE) { |
417 | debug_cpmsd("## %7lu f_read res: %d, bytes rd/wr: %u\n", get_timer(0), res, brw); | |
5f7f3586 L |
418 | dump_ram(disk_buffer, 0, 64, "Read Data"); |
419 | res = -1; | |
420 | } | |
daacc6f9 | 421 | |
7d60b20b | 422 | addr += BLOCK_SIZE; |
5f7f3586 L |
423 | } |
424 | ||
393b1897 L |
425 | if (dowrite && !res) { |
426 | // res = f_sync(&drv_table[drv].fd); | |
427 | drv_table[drv].dirty = true; | |
428 | bg_setstat(handle_cpm_drv_to, 1); | |
429 | } | |
430 | ||
daacc6f9 | 431 | |
5f7f3586 | 432 | if (buserr) { |
7d60b20b | 433 | debug_cpmsd("Bus timeout\n"); |
5f7f3586 L |
434 | rc = 0x03; |
435 | } | |
5f7f3586 L |
436 | |
437 | /* send result*/ | |
393b1897 | 438 | msg_cpm_result(subf, rc, res); |
5f7f3586 L |
439 | } |
440 | ||
72f58822 L |
441 | |
442 | const FLASH struct msg_item z80_messages[] = | |
443 | { | |
444 | { 0, /* fct nr. */ | |
89adce76 | 445 | 1, 3, /* sub fct nr. from, to */ |
72f58822 L |
446 | do_msg_ini_memfifo}, |
447 | { 1, | |
448 | 1, 1, | |
449 | do_msg_char_out}, | |
1a2460dc L |
450 | { 1, |
451 | 2, 2, | |
452 | do_msg_echo}, | |
5f7f3586 L |
453 | { 2, |
454 | 0, 0, | |
455 | do_msg_cpm_login}, | |
456 | { 2, | |
457 | 1, 2, | |
458 | do_msg_cpm_rw}, | |
8590a76b L |
459 | { 3, |
460 | 1, 1, | |
461 | do_msg_get_timer}, | |
845cbdbd L |
462 | { 3, |
463 | 2, 3, /* 2: get, 3: set time and date */ | |
464 | do_msg_get_set_time}, | |
72f58822 L |
465 | { 0xff, /* end mark */ |
466 | 0, 0, | |
467 | 0}, | |
468 | ||
469 | }; | |
470 | ||
471 | ||
472 | ||
473 | ||
474 | void do_message(int len, uint8_t *msg) | |
475 | { | |
476 | uint8_t fct, sub_fct; | |
477 | int_fast8_t i = 0; | |
478 | ||
479 | if (len >= 2) { | |
480 | fct = *msg++; | |
481 | sub_fct = *msg++; | |
482 | len -= 2; | |
483 | ||
89adce76 L |
484 | while (fct != z80_messages[i].fct) { |
485 | if (z80_messages[i].fct == 0xff) { | |
486 | DBG_P(1, "do_message: Unknown function: %i, %i\n", | |
487 | fct, sub_fct); | |
488 | return; /* TODO: unknown message # */ | |
489 | } | |
8a7decea | 490 | |
72f58822 | 491 | ++i; |
72f58822 L |
492 | } |
493 | ||
494 | while (fct == z80_messages[i].fct) { | |
8a7decea | 495 | if (sub_fct >= z80_messages[i].sub_min && |
89adce76 | 496 | sub_fct <= z80_messages[i].sub_max ) |
72f58822 L |
497 | break; |
498 | ++i; | |
499 | } | |
500 | ||
501 | if (z80_messages[i].fct != fct) { | |
502 | DBG_P(1, "do_message: Unknown sub function: %i, %i\n", | |
503 | fct, sub_fct); | |
504 | return; /* TODO: unknown message sub# */ | |
505 | } | |
506 | ||
507 | (z80_messages[i].func)(sub_fct, len, msg); | |
508 | ||
509 | ||
510 | } else { | |
511 | /* TODO: error */ | |
512 | DBG_P(1, "do_message: to few arguments (%i); this shouldn't happen!\n", len); | |
513 | } | |
514 | } | |
515 | ||
516 | ||
517 | ||
518 | #define CTRBUF_LEN 256 | |
519 | ||
520 | void check_msg_fifo(void) | |
521 | { | |
522 | int ch; | |
523 | static int_fast8_t state; | |
524 | static int msglen,idx; | |
525 | static uint8_t buffer[CTRBUF_LEN]; | |
526 | ||
89adce76 | 527 | while ((ch = z80_memfifo_getc(fifo_msgin)) >= 0) { |
72f58822 L |
528 | switch (state) { |
529 | case 0: /* wait for start of message */ | |
3531528e | 530 | if (ch == 0xAE) { /* TODO: magic number */ |
72f58822 L |
531 | msglen = 0; |
532 | idx = 0; | |
533 | state = 1; | |
534 | } | |
535 | break; | |
536 | case 1: /* get msg len */ | |
537 | if (ch > 0 && ch <= CTRBUF_LEN) { | |
538 | msglen = ch; | |
539 | state = 2; | |
540 | } else | |
541 | state = 0; | |
542 | break; | |
543 | case 2: /* get message */ | |
544 | buffer[idx++] = ch; | |
89adce76 L |
545 | if (idx == msglen) { |
546 | do_message(msglen, buffer); | |
547 | state = 0; | |
548 | } | |
549 | break; | |
550 | } | |
551 | } | |
552 | } | |
553 | ||
554 | ||
555 | int msg_handling(int state) | |
556 | { | |
50939dec | 557 | bool pending; |
8a7decea L |
558 | |
559 | ATOMIC_BLOCK(ATOMIC_FORCEON) { | |
89adce76 L |
560 | pending = (Stat & S_MSG_PENDING) != 0; |
561 | Stat &= ~S_MSG_PENDING; | |
562 | } | |
8a7decea | 563 | |
89adce76 | 564 | if (pending) { |
215ec4b2 L |
565 | uint8_t init_request; |
566 | z80_bus_cmd(Request); | |
567 | init_request = z80_read(0x43); | |
568 | z80_bus_cmd(Release); | |
569 | if ( init_request != 0) { | |
8bbf185e | 570 | /* Get address of fifo 0 */ |
89adce76 | 571 | z80_bus_cmd(Request); |
8bbf185e L |
572 | uint32_t fifo_addr = z80_read(0x40) + |
573 | ((uint16_t) z80_read(0x40+1) << 8) + | |
574 | ((uint32_t) z80_read(0x40+2) << 16); | |
215ec4b2 L |
575 | z80_write(0x43, 0); |
576 | z80_bus_cmd(Release); | |
577 | ||
578 | if (fifo_addr != 0) { | |
579 | z80_memfifo_init(fifo_msgin, fifo_addr); | |
580 | state = 1; | |
581 | } else | |
582 | state = 0; | |
583 | ||
584 | } else { | |
89adce76 | 585 | check_msg_fifo(); |
72f58822 L |
586 | } |
587 | } | |
588 | ||
89adce76 L |
589 | return state; |
590 | } | |
89adce76 L |
591 | |
592 | ||
593 | static int handle_msg_handling; | |
594 | ||
595 | void setup_z180_serv(void) | |
596 | { | |
8a7decea | 597 | |
89adce76 | 598 | handle_msg_handling = bg_register(msg_handling, 0); |
393b1897 | 599 | handle_cpm_drv_to = bg_register(cpm_drv_to, 0); |
72f58822 L |
600 | } |
601 | ||
89adce76 L |
602 | void restart_z180_serv(void) |
603 | { | |
604 | z80_bus_cmd(Request); | |
215ec4b2 | 605 | z80_memset(0x40, 0, 4); |
89adce76 | 606 | z80_bus_cmd(Release); |
8a7decea L |
607 | |
608 | for (int i = 0; i < NUM_FIFOS; i++) | |
609 | z80_memfifo_init(i, 0); | |
89adce76 | 610 | bg_setstat(handle_msg_handling, 0); |
5f7f3586 | 611 | |
89adce76 | 612 | } |
72f58822 | 613 | |
845cbdbd | 614 | #if 0 |
72f58822 L |
615 | /*--------------------------------------------------------------------------*/ |
616 | ||
72f58822 L |
617 | const FLASH uint8_t iniprog[] = { |
618 | 0xAF, // xor a | |
619 | 0xED, 0x39, 0x36, // out0 (rcr),a ;disable DRAM refresh | |
620 | 0x3E, 0x30, // ld a,030h | |
621 | 0xED, 0x39, 0x32 //out0 (dcntl),a ;0 mem, max i/0 wait states | |
622 | }; | |
623 | ||
624 | const FLASH uint8_t sertest[] = { | |
625 | 0xAF, // xor a | |
626 | 0xED, 0x39, 0x36, // out0 (rcr),a ;disable DRAM refresh | |
627 | 0x3E, 0x30, // ld a,030h | |
628 | 0xED, 0x39, 0x32, // out0 (dcntl),a ;0 mem, max i/0 wait states | |
629 | 0x3E, 0x80, // ld a,M_MPBT ;no MP, PS=10, DR=16, SS=0 | |
630 | 0xED, 0x39, 0x03, // out0 (cntlb1),a | |
631 | 0x3E, 0x64, // ld a,M_RE + M_TE + M_MOD2 ; | |
632 | 0xED, 0x39, 0x01, // out0 (cntla1),a | |
633 | 0x3E, 0x00, // ld a,0 | |
634 | 0xED, 0x39, 0x05, // out0 (stat1),a ;Enable rx interrupts | |
635 | 0xED, 0x38, 0x05, //l0:in0 a,(stat1) | |
636 | 0xE6, 0x80, // and 80h | |
637 | 0x28, 0xF9, // jr z,l0 | |
638 | 0xED, 0x00, 0x09, // in0 b,(rdr1) | |
639 | 0xED, 0x38, 0x05, //l1:in0 a,(stat1) | |
640 | 0xE6, 0x02, // and 02h | |
641 | 0x28, 0xF9, // jr z,l1 | |
642 | 0xED, 0x01, 0x07, // out0 (tdr1),b | |
643 | 0x18, 0xEA, // jr l0 | |
644 | }; | |
645 | ||
646 | const FLASH uint8_t test1[] = { | |
647 | 0xAF, // xor a | |
648 | 0xED, 0x39, 0x36, // out0 (rcr),a ;disable DRAM refresh | |
649 | 0x3E, 0x30, // ld a,030h | |
650 | 0xED, 0x39, 0x32, // out0 (dcntl),a ;0 mem, max i/0 wait states | |
651 | 0x21, 0x1E, 0x00, // ld hl,dmclrt ;load DMA registers | |
652 | 0x06, 0x08, // ld b,dmct_e-dmclrt | |
653 | 0x0E, 0x20, // ld c,sar0l | |
8a7decea | 654 | 0xED, 0x93, // otimr |
72f58822 L |
655 | 0x3E, 0xC3, // ld a,0c3h ;dst +1, src +1, burst |
656 | 0xED, 0x39, 0x31, // out0 (dmode),a ; | |
8a7decea | 657 | 0x3E, 0x62, // ld a,062h ;enable dma0, |
72f58822 L |
658 | 0xED, 0x39, 0x30, //cl_1: out0 (dstat),a ;copy 64k |
659 | 0x18, 0xFB, // jr cl_1 ; | |
8a7decea | 660 | 0x00, 0x00, //dmclrt: dw 0 ;src (inc) |
72f58822 L |
661 | 0x00, // db 0 ;src |
662 | 0x00, 0x00, // dw 0 ;dst (inc), | |
663 | 0x00, // db 0 ;dst | |
664 | 0x00, 0x00, // dw 0 ;count (64k) | |
665 | }; | |
845cbdbd | 666 | #endif |