]> cloudbase.mooo.com Git - z180-stamp.git/blob - avr/z180-serv.c
MAX_DRIVE, BLOCK_SIZE --> config.h; Check if access is beyond CP/M disk image size.
[z180-stamp.git] / avr / z180-serv.c
1 /*
2 * (C) Copyright 2014-2016 Leo C. <erbl259-lmu@yahoo.de>
3 *
4 * SPDX-License-Identifier: GPL-2.0
5 */
6
7 #include "common.h"
8 #include <stdlib.h>
9 #include <string.h>
10 #include <stdbool.h>
11 #include <util/atomic.h>
12
13 #include "config.h"
14 #include "background.h"
15 #include "env.h"
16 #include "ff.h"
17 #include "serial.h"
18 #include "z80-if.h"
19 #include "debug.h"
20 #include "print-utils.h"
21 #include "z180-serv.h"
22 #include "timer.h"
23 #include "time.h"
24 #include "bcd.h"
25 #include "rtc.h"
26
27 #define DEBUG_CPM_SDIO 0 /* set to 1 to debug */
28
29 #define debug_cpmsd(fmt, args...) \
30 debug_cond(DEBUG_CPM_SDIO, fmt, ##args)
31
32
33 /*--------------------------------------------------------------------------*/
34
35 struct msg_item {
36 uint8_t fct;
37 uint8_t sub_min, sub_max;
38 void (*func)(uint8_t, int, uint8_t *);
39 };
40
41 uint32_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
56
57 static 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
67 int 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
76 void do_msg_ini_memfifo(uint8_t subf, int len, uint8_t * msg)
77 {
78 (void)len;
79
80 z80_memfifo_init(subf, msg_to_addr(msg));
81 }
82
83
84 void 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
92 /* echo message */
93 void 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
101 /* get timer */
102 void do_msg_get_timer(uint8_t subf, int len, uint8_t * msg)
103 {
104 uint32_t time_ms = (len >= 4) ? *(uint32_t *) msg : 0;
105
106 time_ms = get_timer(time_ms);
107 msg_xmit(3, subf, sizeof(time_ms), (uint8_t *) &time_ms);
108 }
109
110 /* ---------------------------------------------------------------------------*/
111
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 */
118 int 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 ;
134 stamp += bcd2bin(msg[2]) * 3600L;
135 gmtime_r(&stamp, tmp);
136 return 0;
137 }
138
139 void 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;
155 }
156
157 /* get/set cp/m time */
158 void 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
171 if (rc >= 0) {
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);
192 if (rc >= 0) {
193 time_t time;
194 time = mk_gmtime(&t);
195 //mktime(&t);
196 gmtime_r(&time, &t);
197
198 mk_cpm_time(&t, cpm_time);
199 } else {
200 my_puts_P(PSTR("## get_time: Get date failed\n"));
201 }
202 break;
203 }
204
205 msg_xmit(3, subf, sizeof(cpm_time), cpm_time);
206 }
207
208 /* ---------------------------------------------------------------------------*/
209
210 /* TODO: Variable Disk Format */
211 #define CONFIG_CPM_DISKSIZE (8*1024*1024L)
212
213 struct cpm_drive_s {
214 uint8_t drv;
215 uint8_t device;
216 char *img_name;
217 bool dirty;
218 FIL fd;
219 };
220
221 static uint8_t disk_buffer[CONFIG_CPM_BLOCK_SIZE];
222 static struct cpm_drive_s drv_table[CONFIG_CPM_MAX_DRIVE];
223 static int handle_cpm_drv_to;
224
225
226 int cpm_drv_to(int state)
227 {
228 static uint32_t ts;
229
230 switch(state) {
231 case 0:
232 break;
233
234 case 1:
235 ts = get_timer(0);
236 state = 2;
237 break;
238
239 case 2:
240 if (get_timer(ts) > 1000) {
241 for (uint_fast8_t i=0; i < CONFIG_CPM_MAX_DRIVE; i++) {
242 if (drv_table[i].dirty) {
243 f_sync(&drv_table[i].fd);
244 drv_table[i].dirty = false;
245 debug_cpmsd("## %7lu f_sync: %c:\n", get_timer(0), i+'A');
246 }
247 }
248 state = 0;
249 }
250 }
251 return state;
252 }
253
254
255 void msg_cpm_result(uint8_t subf, uint8_t rc, int res)
256 {
257 uint8_t result_msg[3];
258
259 if (res)
260 rc |= 0x80;
261
262 result_msg[0] = rc;
263 result_msg[1] = res;
264 result_msg[2] = res >> 8;
265
266 if (rc) {
267 debug_cpmsd("###%7lu error rc: %.02x, res: %d\n", get_timer(0), rc, res);
268 }
269
270 msg_xmit(2, subf, sizeof(result_msg), result_msg);
271 }
272
273 /*
274 db 2 ; disk command
275 ds 1 ; subcommand (login/read/write)
276 ds 1 ; @adrv (8 bits) +0
277 ds 1 ; @rdrv (8 bits) +1
278 ds 3 ; @xdph (24 bits) +2
279 */
280
281 void do_msg_cpm_login(uint8_t subf, int len, uint8_t * msg)
282 {
283
284 FRESULT res = 0;
285 uint8_t drv;
286 char *np;
287
288 (void)subf;
289
290 if (len != 5) { /* TODO: check adrv, rdrv */
291 return msg_cpm_result(subf, 0x01, res);
292 }
293
294 debug_cpmsd("\n## %7lu login: %c:\n", get_timer(0), msg[0]+'A');
295
296
297 drv = msg[0];
298 if ( drv>= CONFIG_CPM_MAX_DRIVE) {
299 return msg_cpm_result(subf, 0x02, res);
300 }
301
302 /*
303 uint32_t dph = ((uint32_t)msg[4] << 16) + ((uint16_t)msg[3] << 8) + msg[2];
304 */
305
306 if (drv_table[drv].img_name != NULL) {
307 debug_cpmsd("## %7lu close: '%s'\n", get_timer(0), drv_table[drv].img_name);
308 f_close(&drv_table[drv].fd);
309 drv_table[drv].dirty = false;
310 free(drv_table[drv].img_name);
311 drv_table[drv].img_name = NULL;
312 }
313
314 strcpy_P((char *)disk_buffer, PSTR("dsk0"));
315 disk_buffer[3] = msg[0] + '0';
316 if (((np = getenv_char((char*)disk_buffer)) == NULL) ||
317 ((drv_table[drv].img_name = strdup(np)) == NULL)) {
318 return msg_cpm_result(subf, 0x03, res);
319 }
320
321
322 res = f_open(&drv_table[drv].fd, drv_table[drv].img_name,
323 FA_WRITE | FA_READ);
324
325 debug_cpmsd("## %7lu open: '%s', (env: '%s'), res: %d\n", get_timer(0),
326 drv_table[drv].img_name, disk_buffer, res);
327
328 /* send result*/
329 msg_cpm_result(subf, 0x00, res);
330 }
331
332
333 /*
334 db 2 ; disk command
335 ds 1 ; subcommand (login/read/write)
336 ds 1 ; @adrv (8 bits) +0
337 ds 1 ; @rdrv (8 bits) +1
338 ds 2 ; @trk (16 bits) +2
339 ds 2 ; @sect(16 bits) +4
340 ds 1 ; @cnt (8 bits) +6
341 ds 3 ; phys. transfer addr +7
342 */
343
344 #define ADRV 0
345 #define RDRV 1
346 #define TRK 2
347 #define SEC 4
348 #define CNT 6
349 #define ADDR 7
350
351 void do_msg_cpm_rw(uint8_t subf, int len, uint8_t * msg)
352 {
353 uint8_t drv;
354 uint32_t addr;
355 uint32_t pos;
356 uint8_t secs;
357 bool dowrite = (subf == 2);
358 FRESULT res = 0;
359 uint8_t rc = 0;
360 bool buserr = 0;
361
362 if (len != 10) { /* TODO: check adrv, rdrv */
363 return msg_cpm_result(subf, 0x01, res);
364 }
365
366 drv = msg[ADRV];
367 if ( drv>= CONFIG_CPM_MAX_DRIVE) {
368 return msg_cpm_result(subf, 0x02, res);
369 }
370
371 secs = msg[CNT];
372 addr = ((uint32_t)msg[ADDR+2] << 16) + ((uint16_t)msg[ADDR+1] << 8) + msg[ADDR];
373
374
375 /* TODO: tracks per sector from dpb */
376 pos = (((uint16_t)(msg[TRK+1] << 8) + msg[TRK]) * 8
377 + ((uint32_t)(msg[SEC+1] << 8) + msg[SEC])) * CONFIG_CPM_BLOCK_SIZE;
378
379 debug_cpmsd("## %7lu cpm_rw: %s %c: trk:%4d, sec: %d, pos: %.8lx, secs: %2d, "
380 "addr: %.5lx\n", get_timer(0), dowrite ? "write" : " read",
381 msg[ADRV]+'A', ((uint16_t)(msg[TRK+1] << 8) + msg[TRK]), msg[SEC],
382 pos, msg[CNT], addr);
383
384 if (pos + secs * CONFIG_CPM_BLOCK_SIZE > CONFIG_CPM_DISKSIZE) {
385 debug_cpmsd(" access > DISKSIZE (%.8lx > %.8lx) aborted!\n",
386 pos + secs * CONFIG_CPM_BLOCK_SIZE, CONFIG_CPM_DISKSIZE);
387 return msg_cpm_result(subf, 0x04, res);
388 }
389
390 res = f_lseek(&drv_table[drv].fd, pos);
391
392 while (!res && secs--) {
393 unsigned int brw;
394 if (dowrite) {
395 if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) {
396 buserr = 1;
397 break;
398 } else {
399 z80_read_block(disk_buffer, addr, CONFIG_CPM_BLOCK_SIZE);
400 z80_bus_cmd(Release);
401 }
402 res = f_write(&drv_table[drv].fd, disk_buffer, CONFIG_CPM_BLOCK_SIZE, &brw);
403 } else {
404 res = f_read(&drv_table[drv].fd, disk_buffer, CONFIG_CPM_BLOCK_SIZE, &brw);
405 if (res == FR_OK) {
406 if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) {
407 buserr = 1;
408 break;
409 } else {
410 z80_write_block(disk_buffer, addr, CONFIG_CPM_BLOCK_SIZE);
411 z80_bus_cmd(Release);
412 }
413 }
414 }
415
416 if (brw != CONFIG_CPM_BLOCK_SIZE) {
417 debug_cpmsd("## %7lu f_read res: %d, bytes rd/wr: %u\n", get_timer(0), res, brw);
418 dump_ram(disk_buffer, 0, 64, "Read Data");
419 res = -1;
420 }
421
422 addr += CONFIG_CPM_BLOCK_SIZE;
423 }
424
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
431
432 if (buserr) {
433 debug_cpmsd("Bus timeout\n");
434 rc = 0x03;
435 }
436
437 /* send result*/
438 msg_cpm_result(subf, rc, res);
439 }
440
441
442 const FLASH struct msg_item z80_messages[] =
443 {
444 { 0, /* fct nr. */
445 1, 3, /* sub fct nr. from, to */
446 do_msg_ini_memfifo},
447 { 1,
448 1, 1,
449 do_msg_char_out},
450 { 1,
451 2, 2,
452 do_msg_echo},
453 { 2,
454 0, 0,
455 do_msg_cpm_login},
456 { 2,
457 1, 2,
458 do_msg_cpm_rw},
459 { 3,
460 1, 1,
461 do_msg_get_timer},
462 { 3,
463 2, 3, /* 2: get, 3: set time and date */
464 do_msg_get_set_time},
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
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 }
490
491 ++i;
492 }
493
494 while (fct == z80_messages[i].fct) {
495 if (sub_fct >= z80_messages[i].sub_min &&
496 sub_fct <= z80_messages[i].sub_max )
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
527 while ((ch = z80_memfifo_getc(fifo_msgin)) >= 0) {
528 switch (state) {
529 case 0: /* wait for start of message */
530 if (ch == 0xAE) { /* TODO: magic number */
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;
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 {
557 bool pending;
558
559 ATOMIC_BLOCK(ATOMIC_FORCEON) {
560 pending = (Stat & S_MSG_PENDING) != 0;
561 Stat &= ~S_MSG_PENDING;
562 }
563
564 if (pending) {
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) {
570 /* Get address of fifo 0 */
571 z80_bus_cmd(Request);
572 uint32_t fifo_addr = z80_read(0x40) +
573 ((uint16_t) z80_read(0x40+1) << 8) +
574 ((uint32_t) z80_read(0x40+2) << 16);
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 {
585 check_msg_fifo();
586 }
587 }
588
589 return state;
590 }
591
592
593 static int handle_msg_handling;
594
595 void setup_z180_serv(void)
596 {
597
598 handle_msg_handling = bg_register(msg_handling, 0);
599 handle_cpm_drv_to = bg_register(cpm_drv_to, 0);
600 }
601
602 void restart_z180_serv(void)
603 {
604 z80_bus_cmd(Request);
605 z80_memset(0x40, 0, 4);
606 z80_bus_cmd(Release);
607
608 for (int i = 0; i < NUM_FIFOS; i++)
609 z80_memfifo_init(i, 0);
610 bg_setstat(handle_msg_handling, 0);
611
612 }
613
614 #if 0
615 /*--------------------------------------------------------------------------*/
616
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
654 0xED, 0x93, // otimr
655 0x3E, 0xC3, // ld a,0c3h ;dst +1, src +1, burst
656 0xED, 0x39, 0x31, // out0 (dmode),a ;
657 0x3E, 0x62, // ld a,062h ;enable dma0,
658 0xED, 0x39, 0x30, //cl_1: out0 (dstat),a ;copy 64k
659 0x18, 0xFB, // jr cl_1 ;
660 0x00, 0x00, //dmclrt: dw 0 ;src (inc)
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 };
666 #endif