]> cloudbase.mooo.com Git - z180-stamp.git/blame - avr/z180-serv.c
"Enhance" CP/M drive login
[z180-stamp.git] / avr / z180-serv.c
CommitLineData
35edb766 1/*
1aed7fd5 2 * (C) Copyright 2014-2016 Leo C. <erbl259-lmu@yahoo.de>
35edb766 3 *
ad12f284 4 * SPDX-License-Identifier: GPL-2.0
35edb766
L
5 */
6
cb4fb1ed 7#include "z180-serv.h"
72f58822 8#include "common.h"
5f7f3586
L
9#include <stdlib.h>
10#include <string.h>
11#include <stdbool.h>
89adce76 12#include <util/atomic.h>
72f58822 13
1aed7fd5 14#include "config.h"
89adce76 15#include "background.h"
5f7f3586
L
16#include "env.h"
17#include "ff.h"
72f58822
L
18#include "serial.h"
19#include "z80-if.h"
889202c4 20#include "debug.h"
5f7f3586 21#include "print-utils.h"
daacc6f9 22#include "timer.h"
845cbdbd
L
23#include "time.h"
24#include "bcd.h"
25#include "rtc.h"
7d60b20b 26
2ca8d8c7 27#define DEBUG_CPM_SDIO 0 /* set to 1 to debug */
7d60b20b
L
28
29#define debug_cpmsd(fmt, args...) \
30 debug_cond(DEBUG_CPM_SDIO, fmt, ##args)
31
32
72f58822
L
33/*--------------------------------------------------------------------------*/
34
35struct msg_item {
36 uint8_t fct;
37 uint8_t sub_min, sub_max;
38 void (*func)(uint8_t, int, uint8_t *);
39};
40
41uint32_t msg_to_addr(uint8_t *msg)
42{
43 union {
44 uint32_t as32;
45 uint8_t as8[4];
46 } addr;
47
48 addr.as8[0] = msg[0];
49 addr.as8[1] = msg[1];
50 addr.as8[2] = msg[2];
51 addr.as8[3] = 0;
52
53 return addr.as32;
54}
55
72f58822 56
1a2460dc
L
57static int msg_xmit_header(uint8_t func, uint8_t subf, int len)
58{
59 z80_memfifo_putc(fifo_msgout, 0xAE);
60 z80_memfifo_putc(fifo_msgout, len+2);
61 z80_memfifo_putc(fifo_msgout, func);
62 z80_memfifo_putc(fifo_msgout, subf);
63
64 return 0;
65}
66
67int msg_xmit(uint8_t func, uint8_t subf, int len, uint8_t *msg)
68{
69 msg_xmit_header(func, subf, len);
70 while (len--)
71 z80_memfifo_putc(fifo_msgout, *msg++);
72
73 return 0;
74}
75
72f58822
L
76void do_msg_ini_memfifo(uint8_t subf, int len, uint8_t * msg)
77{
78 (void)len;
79
89adce76 80 z80_memfifo_init(subf, msg_to_addr(msg));
72f58822
L
81}
82
83
84void do_msg_char_out(uint8_t subf, int len, uint8_t * msg)
85{
86 (void)subf;
87
88 while (len--)
89 putchar(*msg++);
90}
91
1a2460dc
L
92/* echo message */
93void do_msg_echo(uint8_t subf, int len, uint8_t * msg)
94{
95 (void)subf;
96
97 /* send re-echo */
98 msg_xmit(1, 3, len, msg);
99}
100
8590a76b
L
101/* get timer */
102void do_msg_get_timer(uint8_t subf, int len, uint8_t * msg)
103{
a8eb521f 104 uint32_t time_ms = (len >= 4) ? *(uint32_t *) msg : 0;
8590a76b 105
a8eb521f 106 time_ms = get_timer(time_ms);
8590a76b
L
107 msg_xmit(3, subf, sizeof(time_ms), (uint8_t *) &time_ms);
108}
109
5f7f3586
L
110/* ---------------------------------------------------------------------------*/
111
845cbdbd
L
112#define CPM_DAY_OFFSET ((1978-1900) * 365 + 19) /* 19 leap years */
113
114/*
115 * Convert CP/M time stamp to a broken-down time structure
116 *
117 */
118int mk_date_time (int len, uint8_t *msg, struct tm *tmp)
119{
120 time_t stamp;
121
122 if (len != 5)
123 return -1;
124
125 /* days since 2000-01-01 */
126 long days = msg[3] + (msg[4] << 8) - 8036;
127
128 if (days < 0)
129 return -1;
130
131 stamp = days * ONE_DAY;
132 stamp += bcd2bin(msg[0]);
133 stamp += bcd2bin(msg[1]) * 60 ;
8d0fad4c 134 stamp += bcd2bin(msg[2]) * 3600L;
845cbdbd
L
135 gmtime_r(&stamp, tmp);
136 return 0;
137}
138
139void mk_cpm_time(struct tm *tmp, uint8_t cpm_time[5])
140{
141 uint16_t days = 1;
142 uint_fast8_t leap=2;
143
144 for (int year=78; year < tmp->tm_year; year++) {
145 days = days + 365 + (leap == 0);
146 leap = (leap+1)%4;
147 }
148 days += tmp->tm_yday;
149
150 cpm_time[0] = bin2bcd(tmp->tm_sec);
151 cpm_time[1] = bin2bcd(tmp->tm_min);
152 cpm_time[2] = bin2bcd(tmp->tm_hour);
153 cpm_time[3] = days;
154 cpm_time[4] = days >> 8;
845cbdbd
L
155}
156
157/* get/set cp/m time */
158void do_msg_get_set_time(uint8_t subf, int len, uint8_t * msg)
159{
160 struct tm t;
161 uint8_t cpm_time[5];
162 int rc;
163
164 memset(cpm_time, 0, ARRAY_SIZE(cpm_time));
165
166 switch (subf) {
167 case 3: /* set date & time */
168 /* initialize t with current time */
169 rc = rtc_get (&t);
170
85046f8c 171 if (rc >= 0) {
845cbdbd
L
172 /* insert new date & time */
173 if (mk_date_time (len, msg, &t) != 0) {
174 my_puts_P(PSTR("## set_time: Bad date format\n"));
175 break;
176 }
177
178 time_t time;
179 time = mk_gmtime(&t);
180 gmtime_r(&time, &t);
181
182 /* and write to RTC */
183 rc = rtc_set (&t);
184 if(rc)
185 my_puts_P(PSTR("## set_time: Set date failed\n"));
186 } else {
187 my_puts_P(PSTR("## set_time: Get date failed\n"));
188 }
189 /* FALL TROUGH */
190 case 2: /* get date & time */
191 rc = rtc_get (&t);
85046f8c
L
192 if (rc >= 0) {
193 time_t time;
194 time = mk_gmtime(&t);
195 //mktime(&t);
196 gmtime_r(&time, &t);
845cbdbd 197
85046f8c
L
198 mk_cpm_time(&t, cpm_time);
199 } else {
845cbdbd 200 my_puts_P(PSTR("## get_time: Get date failed\n"));
845cbdbd 201 }
845cbdbd
L
202 break;
203 }
204
205 msg_xmit(3, subf, sizeof(cpm_time), cpm_time);
206}
207
208/* ---------------------------------------------------------------------------*/
209
1aed7fd5
L
210static uint8_t disk_buffer[CONFIG_CPM_BLOCK_SIZE];
211static struct cpm_drive_s drv_table[CONFIG_CPM_MAX_DRIVE];
393b1897
L
212static int handle_cpm_drv_to;
213
393b1897 214
9461ecf3
L
215void drv_debug(uint8_t drv, const FLASH char *const fmt, ...) \
216{
217 struct cpm_drive_s *dp = &drv_table[drv];
218
219 if (dp->opt & DRV_OPT_DEBUG) {
220
221 va_list ap;
222
223 printf_P(PSTR("# %7lu dsk%d: "), get_timer(0), drv);
224
225 va_start (ap, fmt);
226 vfprintf_P (stdout, fmt, ap);
227 va_end (ap);
228
229 putc('\n', stdout);
230 }
231}
232
cb4fb1ed
L
233int drv_list(void)
234{
235 for (uint8_t i = 0; i < CONFIG_CPM_MAX_DRIVE; i++) {
236 struct cpm_drive_s * p = &drv_table[i];
237 if (p->img_name) {
238 printf_P(PSTR(" dsk%d: %2s %3s attached to %s\n"), i,
239 p->opt&DRV_OPT_RO ? "RO":"RW", p->opt&DRV_OPT_DEBUG ? "DBG":"",
240 p->img_name);
241 }
242 }
243 return 0;
244}
245
246int drv_detach(uint8_t drv)
247{
248 if (drv < CONFIG_CPM_MAX_DRIVE) {
249 struct cpm_drive_s *p = &drv_table[drv];
250
9461ecf3 251 drv_debug(drv, PSTR("detach from '%s'"), p->img_name ? p->img_name : "-");
cb4fb1ed
L
252
253 if (p->img_name) {
254 f_close(&p->fd);
cb4fb1ed 255 free(p->img_name);
4122fe90
L
256 p->opt = 0;
257 p->flags &= ~DRV_FLG_DIRTY;
cb4fb1ed 258 p->img_name = NULL;
e34ae619
L
259
260 uint32_t scb = getenv_ulong(ENV_CPM3_SCB, 16, 0);
261 if (scb && (z80_bus_cmd(Request) & ZST_ACQUIRED)) {
262 z80_write(scb + 0xf0, 0xff);
263 z80_write(p->dph + 11, 0xff);
264 z80_bus_cmd(Release);
265 }
cb4fb1ed
L
266 }
267 }
268 return 0;
269}
270
271static int drv_find_file_attached(const char *fn)
272{
273 for (uint8_t i = 0; i < CONFIG_CPM_MAX_DRIVE; i++) {
274 struct cpm_drive_s *p = &drv_table[i];
275 if (p->img_name && !strcmp(fn, p->img_name)) {
276 return i;
277 }
278 }
279 return -1;
280}
281
282int drv_attach(uint8_t drv, const char *filename, drv_opt_t options)
283{
284 int res;
285
286 if (drv >= CONFIG_CPM_MAX_DRIVE)
287 return AT_RANGE;
288
289 struct cpm_drive_s *p = &drv_table[drv];
290
291 if (options & DRV_OPT_REATTATCH) {
292 if (filename) {
293 return AT_ERROR;
294 }
295
296 if (!p->img_name) {
297 return AT_NOT;
298 }
299
9baecc6b
L
300 /* change options */
301 if ((p->opt ^ options) & DRV_OPT_RO) {
302 f_close(&p->fd);
303 res = f_open(&p->fd, p->img_name,
304 FA_READ | (options&DRV_OPT_RO ? 0 : FA_WRITE));
305 }
cb4fb1ed 306
9baecc6b 307 p->opt = options & ~DRV_OPT_REATTATCH;
cb4fb1ed
L
308
309 } else {
310
311 if (p->img_name)
312 return AT_ALREADY;
313 if (drv_find_file_attached(filename) >= 0)
314 return AT_OTHER;
315
4122fe90 316 p->opt = options;
cb4fb1ed
L
317
318 /* new attachment */
319
320 if ((p->img_name = strdup(filename)) == NULL)
321 return AT_NOMEM;
322
323 res = f_open(&p->fd, p->img_name,
9baecc6b 324 FA_READ | (options&DRV_OPT_RO ? 0 : FA_WRITE));
cb4fb1ed
L
325
326 if (!res && f_size(&p->fd) < CONFIG_CPM_DISKSIZE) {
9baecc6b 327#if 0
cb4fb1ed 328 unsigned int bw;
cb4fb1ed
L
329 debug_cpmsd(" expanding image file from %ld to %ld\n",
330 f_size(&p->fd), CONFIG_CPM_DISKSIZE);
331
332 res = f_lseek(&p->fd, CONFIG_CPM_DISKSIZE-CONFIG_CPM_BLOCK_SIZE);
333 if (!res) {
334 memset(disk_buffer, 0xe5, CONFIG_CPM_BLOCK_SIZE);
335 res = f_write(&p->fd, disk_buffer, CONFIG_CPM_BLOCK_SIZE, &bw);
336 if (res || bw < CONFIG_CPM_BLOCK_SIZE) {
337 debug_cpmsd(" failed! res: %d, bytes written: %u\n", res, bw);
338 }
4122fe90 339 p->flags |= DRV_FLG_DIRTY;
cb4fb1ed
L
340 bg_setstat(handle_cpm_drv_to, 1);
341 }
9baecc6b 342#else
9461ecf3 343 drv_debug(drv, PSTR("wrong image file size: %ld, should be %ld"),
9baecc6b
L
344 f_size(&p->fd), CONFIG_CPM_DISKSIZE);
345 res = 64;
346#endif
cb4fb1ed
L
347 }
348 if (res) {
349 drv_detach(drv);
350 return AT_OPEN;
351 }
352 }
353
354 return AT_OK;
355}
356
357
393b1897
L
358int cpm_drv_to(int state)
359{
360 static uint32_t ts;
361
362 switch(state) {
363 case 0:
364 break;
365
366 case 1:
367 ts = get_timer(0);
368 state = 2;
369 break;
370
371 case 2:
372 if (get_timer(ts) > 1000) {
1aed7fd5 373 for (uint_fast8_t i=0; i < CONFIG_CPM_MAX_DRIVE; i++) {
4122fe90 374 if (drv_table[i].flags & DRV_FLG_DIRTY) {
4122fe90 375 drv_table[i].flags &= ~DRV_FLG_DIRTY;
9461ecf3
L
376 f_sync(&drv_table[i].fd);
377 drv_debug(i, PSTR("f_sync"));
393b1897
L
378 }
379 }
380 state = 0;
381 }
382 }
383 return state;
384}
385
386
387void msg_cpm_result(uint8_t subf, uint8_t rc, int res)
388{
389 uint8_t result_msg[3];
390
391 if (res)
392 rc |= 0x80;
393
394 result_msg[0] = rc;
395 result_msg[1] = res;
396 result_msg[2] = res >> 8;
397
398 if (rc) {
399 debug_cpmsd("###%7lu error rc: %.02x, res: %d\n", get_timer(0), rc, res);
400 }
401
402 msg_xmit(2, subf, sizeof(result_msg), result_msg);
403}
5f7f3586
L
404
405/*
406 db 2 ; disk command
407 ds 1 ; subcommand (login/read/write)
408 ds 1 ; @adrv (8 bits) +0
409 ds 1 ; @rdrv (8 bits) +1
410 ds 3 ; @xdph (24 bits) +2
411*/
412
413void do_msg_cpm_login(uint8_t subf, int len, uint8_t * msg)
414{
5f7f3586 415 uint8_t drv;
9baecc6b 416 struct cpm_drive_s *dp;
4122fe90 417 FRESULT res = 0;
5f7f3586
L
418
419 (void)subf;
420
afc7c4b4 421 if (len != 5) {
393b1897 422 return msg_cpm_result(subf, 0x01, res);
5f7f3586
L
423 }
424
afc7c4b4
L
425 /* Get relative drive number */
426 drv = msg[1];
cb4fb1ed 427 if ( drv >= CONFIG_CPM_MAX_DRIVE) {
9461ecf3 428 debug_cpmsd("## login failed: invalid relative drive number '%d'\n", drv);
393b1897 429 return msg_cpm_result(subf, 0x02, res);
5f7f3586
L
430 }
431
9461ecf3
L
432 drv_debug(drv, PSTR("login"));
433
9baecc6b
L
434 dp = &drv_table[drv];
435 dp->flags &= ~DRV_FLG_OPEN;
436 dp->dph = ((uint32_t)msg[4] << 16) + ((uint16_t)msg[3] << 8) + msg[2];
cb4fb1ed 437
5f7f3586 438
9baecc6b 439 if (dp->img_name == NULL) {
4122fe90 440 /* no file attached */
9461ecf3 441 drv_debug(drv, PSTR("login failed: no file attached"));
393b1897 442 return msg_cpm_result(subf, 0x03, res);
5f7f3586
L
443 }
444
019dc5f9
L
445 f_close(&dp->fd);
446 res = f_open(&dp->fd, dp->img_name,
447 FA_READ | (dp->opt&DRV_OPT_RO ? 0 : FA_WRITE));
448
9baecc6b 449 dp->flags |= DRV_FLG_OPEN;
afc7c4b4 450
5f7f3586 451 /* send result*/
393b1897 452 msg_cpm_result(subf, 0x00, res);
5f7f3586
L
453}
454
455
456/*
457 db 2 ; disk command
458 ds 1 ; subcommand (login/read/write)
daacc6f9
L
459 ds 1 ; @adrv (8 bits) +0
460 ds 1 ; @rdrv (8 bits) +1
461 ds 2 ; @trk (16 bits) +2
462 ds 2 ; @sect(16 bits) +4
463 ds 1 ; @cnt (8 bits) +6
5f7f3586
L
464 ds 3 ; phys. transfer addr +7
465*/
466
daacc6f9
L
467#define ADRV 0
468#define RDRV 1
469#define TRK 2
470#define SEC 4
471#define CNT 6
472#define ADDR 7
473
5f7f3586
L
474void do_msg_cpm_rw(uint8_t subf, int len, uint8_t * msg)
475{
476 uint8_t drv;
9baecc6b 477 struct cpm_drive_s *dp;
5f7f3586
L
478 uint32_t addr;
479 uint32_t pos;
9461ecf3
L
480 uint16_t track;
481 uint16_t sec;
7d60b20b 482 uint8_t secs;
9baecc6b 483 bool dowrite;
5f7f3586
L
484 FRESULT res = 0;
485 uint8_t rc = 0;
486 bool buserr = 0;
5f7f3586 487
afc7c4b4 488 if (len != 10) {
393b1897 489 return msg_cpm_result(subf, 0x01, res);
5f7f3586
L
490 }
491
afc7c4b4 492 drv = msg[RDRV];
1aed7fd5 493 if ( drv>= CONFIG_CPM_MAX_DRIVE) {
393b1897 494 return msg_cpm_result(subf, 0x02, res);
5f7f3586
L
495 }
496
9baecc6b
L
497 dp = &drv_table[drv];
498
499 if (dp->img_name == NULL) {
500 /* no media */
501 return msg_cpm_result(subf, 0x06, res);
502 }
503
504 if (subf == 2) {
505 dowrite = true;
506 if (dp->opt & DRV_OPT_RO) {
507 return msg_cpm_result(subf, 0x05, res);
508 }
509 } else {
510 dowrite = false;
511 }
512
9461ecf3
L
513 track = (uint16_t)(msg[TRK+1] << 8) + msg[TRK];
514 sec = (uint16_t)(msg[SEC+1] << 8) + msg[SEC];
7d60b20b 515 secs = msg[CNT];
daacc6f9 516 addr = ((uint32_t)msg[ADDR+2] << 16) + ((uint16_t)msg[ADDR+1] << 8) + msg[ADDR];
5f7f3586 517
5f7f3586 518
daacc6f9 519 /* TODO: tracks per sector from dpb */
9461ecf3 520 pos = (track * 8UL + sec) * CONFIG_CPM_BLOCK_SIZE;
5f7f3586 521
9461ecf3
L
522 drv_debug(drv, PSTR("%5s trk:%4d, sec: %d, pos: %.8lx, secs: %2d, addr: %.5lx"),
523 dowrite ? "write" : " read", track, sec, pos, secs, addr);
5f7f3586 524
1aed7fd5 525 if (pos + secs * CONFIG_CPM_BLOCK_SIZE > CONFIG_CPM_DISKSIZE) {
9461ecf3 526 drv_debug(drv, PSTR("access > DISKSIZE (%.8lx > %.8lx) aborted!"),
1aed7fd5
L
527 pos + secs * CONFIG_CPM_BLOCK_SIZE, CONFIG_CPM_DISKSIZE);
528 return msg_cpm_result(subf, 0x04, res);
529 }
530
9baecc6b 531 res = f_lseek(&dp->fd, pos);
1aed7fd5 532
7d60b20b 533 while (!res && secs--) {
8bbf185e 534 unsigned int brw;
5f7f3586
L
535 if (dowrite) {
536 if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) {
537 buserr = 1;
7d60b20b 538 break;
5f7f3586 539 } else {
1aed7fd5 540 z80_read_block(disk_buffer, addr, CONFIG_CPM_BLOCK_SIZE);
5f7f3586 541 z80_bus_cmd(Release);
5f7f3586 542 }
9baecc6b 543 res = f_write(&dp->fd, disk_buffer, CONFIG_CPM_BLOCK_SIZE, &brw);
5f7f3586 544 } else {
9baecc6b 545 res = f_read(&dp->fd, disk_buffer, CONFIG_CPM_BLOCK_SIZE, &brw);
1aed7fd5 546 if (res == FR_OK) {
5f7f3586
L
547 if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) {
548 buserr = 1;
7d60b20b 549 break;
5f7f3586 550 } else {
afc7c4b4 551 z80_write_block(disk_buffer, addr, CONFIG_CPM_BLOCK_SIZE);
5f7f3586
L
552 z80_bus_cmd(Release);
553 }
554 }
555 }
1aed7fd5 556 if (brw != CONFIG_CPM_BLOCK_SIZE) {
9461ecf3 557 drv_debug(drv, PSTR("short read or write: res: %d, bytes rd/wr: %u"), res, brw);
afc7c4b4 558 res = 64;
5f7f3586 559 }
1aed7fd5 560 addr += CONFIG_CPM_BLOCK_SIZE;
5f7f3586
L
561 }
562
393b1897 563 if (dowrite && !res) {
9baecc6b 564 dp->flags |= DRV_FLG_DIRTY;
393b1897
L
565 bg_setstat(handle_cpm_drv_to, 1);
566 }
567
5f7f3586 568 if (buserr) {
9461ecf3 569 drv_debug(drv, PSTR("Bus timeout"));
5f7f3586
L
570 rc = 0x03;
571 }
5f7f3586
L
572
573 /* send result*/
393b1897 574 msg_cpm_result(subf, rc, res);
5f7f3586
L
575}
576
72f58822
L
577
578const FLASH struct msg_item z80_messages[] =
579{
580 { 0, /* fct nr. */
89adce76 581 1, 3, /* sub fct nr. from, to */
72f58822
L
582 do_msg_ini_memfifo},
583 { 1,
584 1, 1,
585 do_msg_char_out},
1a2460dc
L
586 { 1,
587 2, 2,
588 do_msg_echo},
5f7f3586
L
589 { 2,
590 0, 0,
591 do_msg_cpm_login},
592 { 2,
593 1, 2,
594 do_msg_cpm_rw},
8590a76b
L
595 { 3,
596 1, 1,
597 do_msg_get_timer},
845cbdbd
L
598 { 3,
599 2, 3, /* 2: get, 3: set time and date */
600 do_msg_get_set_time},
72f58822
L
601 { 0xff, /* end mark */
602 0, 0,
603 0},
604
605};
606
607
608
609
610void do_message(int len, uint8_t *msg)
611{
612 uint8_t fct, sub_fct;
613 int_fast8_t i = 0;
614
615 if (len >= 2) {
616 fct = *msg++;
617 sub_fct = *msg++;
618 len -= 2;
619
89adce76
L
620 while (fct != z80_messages[i].fct) {
621 if (z80_messages[i].fct == 0xff) {
622 DBG_P(1, "do_message: Unknown function: %i, %i\n",
623 fct, sub_fct);
624 return; /* TODO: unknown message # */
625 }
8a7decea 626
72f58822 627 ++i;
72f58822
L
628 }
629
630 while (fct == z80_messages[i].fct) {
8a7decea 631 if (sub_fct >= z80_messages[i].sub_min &&
89adce76 632 sub_fct <= z80_messages[i].sub_max )
72f58822
L
633 break;
634 ++i;
635 }
636
637 if (z80_messages[i].fct != fct) {
638 DBG_P(1, "do_message: Unknown sub function: %i, %i\n",
639 fct, sub_fct);
640 return; /* TODO: unknown message sub# */
641 }
642
643 (z80_messages[i].func)(sub_fct, len, msg);
644
645
646 } else {
647 /* TODO: error */
648 DBG_P(1, "do_message: to few arguments (%i); this shouldn't happen!\n", len);
649 }
650}
651
652
653
654#define CTRBUF_LEN 256
655
656void check_msg_fifo(void)
657{
658 int ch;
659 static int_fast8_t state;
660 static int msglen,idx;
661 static uint8_t buffer[CTRBUF_LEN];
662
89adce76 663 while ((ch = z80_memfifo_getc(fifo_msgin)) >= 0) {
72f58822
L
664 switch (state) {
665 case 0: /* wait for start of message */
3531528e 666 if (ch == 0xAE) { /* TODO: magic number */
72f58822
L
667 msglen = 0;
668 idx = 0;
669 state = 1;
670 }
671 break;
672 case 1: /* get msg len */
673 if (ch > 0 && ch <= CTRBUF_LEN) {
674 msglen = ch;
675 state = 2;
676 } else
677 state = 0;
678 break;
679 case 2: /* get message */
680 buffer[idx++] = ch;
89adce76
L
681 if (idx == msglen) {
682 do_message(msglen, buffer);
683 state = 0;
684 }
685 break;
686 }
687 }
688}
689
690
691int msg_handling(int state)
692{
50939dec 693 bool pending;
8a7decea
L
694
695 ATOMIC_BLOCK(ATOMIC_FORCEON) {
89adce76
L
696 pending = (Stat & S_MSG_PENDING) != 0;
697 Stat &= ~S_MSG_PENDING;
698 }
8a7decea 699
89adce76 700 if (pending) {
215ec4b2
L
701 uint8_t init_request;
702 z80_bus_cmd(Request);
703 init_request = z80_read(0x43);
704 z80_bus_cmd(Release);
705 if ( init_request != 0) {
8bbf185e 706 /* Get address of fifo 0 */
89adce76 707 z80_bus_cmd(Request);
8bbf185e
L
708 uint32_t fifo_addr = z80_read(0x40) +
709 ((uint16_t) z80_read(0x40+1) << 8) +
710 ((uint32_t) z80_read(0x40+2) << 16);
215ec4b2
L
711 z80_write(0x43, 0);
712 z80_bus_cmd(Release);
713
714 if (fifo_addr != 0) {
715 z80_memfifo_init(fifo_msgin, fifo_addr);
716 state = 1;
717 } else
718 state = 0;
719
720 } else {
89adce76 721 check_msg_fifo();
72f58822
L
722 }
723 }
724
89adce76
L
725 return state;
726}
89adce76
L
727
728
729static int handle_msg_handling;
730
731void setup_z180_serv(void)
732{
8a7decea 733
89adce76 734 handle_msg_handling = bg_register(msg_handling, 0);
393b1897 735 handle_cpm_drv_to = bg_register(cpm_drv_to, 0);
72f58822
L
736}
737
89adce76
L
738void restart_z180_serv(void)
739{
740 z80_bus_cmd(Request);
215ec4b2 741 z80_memset(0x40, 0, 4);
89adce76 742 z80_bus_cmd(Release);
8a7decea
L
743
744 for (int i = 0; i < NUM_FIFOS; i++)
745 z80_memfifo_init(i, 0);
89adce76 746 bg_setstat(handle_msg_handling, 0);
5f7f3586 747
89adce76 748}
72f58822 749
845cbdbd 750#if 0
72f58822
L
751/*--------------------------------------------------------------------------*/
752
72f58822
L
753const FLASH uint8_t iniprog[] = {
754 0xAF, // xor a
755 0xED, 0x39, 0x36, // out0 (rcr),a ;disable DRAM refresh
756 0x3E, 0x30, // ld a,030h
757 0xED, 0x39, 0x32 //out0 (dcntl),a ;0 mem, max i/0 wait states
758};
759
760const FLASH uint8_t sertest[] = {
761 0xAF, // xor a
762 0xED, 0x39, 0x36, // out0 (rcr),a ;disable DRAM refresh
763 0x3E, 0x30, // ld a,030h
764 0xED, 0x39, 0x32, // out0 (dcntl),a ;0 mem, max i/0 wait states
765 0x3E, 0x80, // ld a,M_MPBT ;no MP, PS=10, DR=16, SS=0
766 0xED, 0x39, 0x03, // out0 (cntlb1),a
767 0x3E, 0x64, // ld a,M_RE + M_TE + M_MOD2 ;
768 0xED, 0x39, 0x01, // out0 (cntla1),a
769 0x3E, 0x00, // ld a,0
770 0xED, 0x39, 0x05, // out0 (stat1),a ;Enable rx interrupts
771 0xED, 0x38, 0x05, //l0:in0 a,(stat1)
772 0xE6, 0x80, // and 80h
773 0x28, 0xF9, // jr z,l0
774 0xED, 0x00, 0x09, // in0 b,(rdr1)
775 0xED, 0x38, 0x05, //l1:in0 a,(stat1)
776 0xE6, 0x02, // and 02h
777 0x28, 0xF9, // jr z,l1
778 0xED, 0x01, 0x07, // out0 (tdr1),b
779 0x18, 0xEA, // jr l0
780};
781
782const FLASH uint8_t test1[] = {
783 0xAF, // xor a
784 0xED, 0x39, 0x36, // out0 (rcr),a ;disable DRAM refresh
785 0x3E, 0x30, // ld a,030h
786 0xED, 0x39, 0x32, // out0 (dcntl),a ;0 mem, max i/0 wait states
787 0x21, 0x1E, 0x00, // ld hl,dmclrt ;load DMA registers
788 0x06, 0x08, // ld b,dmct_e-dmclrt
789 0x0E, 0x20, // ld c,sar0l
8a7decea 790 0xED, 0x93, // otimr
72f58822
L
791 0x3E, 0xC3, // ld a,0c3h ;dst +1, src +1, burst
792 0xED, 0x39, 0x31, // out0 (dmode),a ;
8a7decea 793 0x3E, 0x62, // ld a,062h ;enable dma0,
72f58822
L
794 0xED, 0x39, 0x30, //cl_1: out0 (dstat),a ;copy 64k
795 0x18, 0xFB, // jr cl_1 ;
8a7decea 796 0x00, 0x00, //dmclrt: dw 0 ;src (inc)
72f58822
L
797 0x00, // db 0 ;src
798 0x00, 0x00, // dw 0 ;dst (inc),
799 0x00, // db 0 ;dst
800 0x00, 0x00, // dw 0 ;count (64k)
801};
845cbdbd 802#endif