]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/z180-serv.c
2 * (C) Copyright 2014-2016 Leo C. <erbl259-lmu@yahoo.de>
4 * SPDX-License-Identifier: GPL-2.0
12 #include <util/atomic.h>
15 #include "background.h"
21 #include "print-utils.h"
27 #define DEBUG_CPM_SDIO 1 /* set to 1 to debug */
29 #define debug_cpmsd(fmt, args...) \
30 debug_cond(DEBUG_CPM_SDIO, fmt, ##args)
33 /*--------------------------------------------------------------------------*/
37 uint8_t sub_min
, sub_max
;
38 void (*func
)(uint8_t, int, uint8_t *);
41 uint32_t msg_to_addr(uint8_t *msg
)
57 static int msg_xmit_header(uint8_t func
, uint8_t subf
, int len
)
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
);
67 int msg_xmit(uint8_t func
, uint8_t subf
, int len
, uint8_t *msg
)
69 msg_xmit_header(func
, subf
, len
);
71 z80_memfifo_putc(fifo_msgout
, *msg
++);
76 void do_msg_ini_memfifo(uint8_t subf
, int len
, uint8_t * msg
)
80 z80_memfifo_init(subf
, msg_to_addr(msg
));
84 void do_msg_char_out(uint8_t subf
, int len
, uint8_t * msg
)
93 void do_msg_echo(uint8_t subf
, int len
, uint8_t * msg
)
98 msg_xmit(1, 3, len
, msg
);
102 void do_msg_get_timer(uint8_t subf
, int len
, uint8_t * msg
)
104 uint32_t time_ms
= (len
>= 4) ? *(uint32_t *) msg
: 0;
106 time_ms
= get_timer(time_ms
);
107 msg_xmit(3, subf
, sizeof(time_ms
), (uint8_t *) &time_ms
);
110 /* ---------------------------------------------------------------------------*/
112 #define CPM_DAY_OFFSET ((1978-1900) * 365 + 19) /* 19 leap years */
115 * Convert CP/M time stamp to a broken-down time structure
118 int mk_date_time (int len
, uint8_t *msg
, struct tm
*tmp
)
125 /* days since 2000-01-01 */
126 long days
= msg
[3] + (msg
[4] << 8) - 8036;
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
);
139 void mk_cpm_time(struct tm
*tmp
, uint8_t cpm_time
[5])
144 for (int year
=78; year
< tmp
->tm_year
; year
++) {
145 days
= days
+ 365 + (leap
== 0);
148 days
+= tmp
->tm_yday
;
150 cpm_time
[0] = bin2bcd(tmp
->tm_sec
);
151 cpm_time
[1] = bin2bcd(tmp
->tm_min
);
152 cpm_time
[2] = bin2bcd(tmp
->tm_hour
);
154 cpm_time
[4] = days
>> 8;
157 /* get/set cp/m time */
158 void do_msg_get_set_time(uint8_t subf
, int len
, uint8_t * msg
)
164 memset(cpm_time
, 0, ARRAY_SIZE(cpm_time
));
167 case 3: /* set date & time */
168 /* initialize t with current time */
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"));
179 time
= mk_gmtime(&t
);
182 /* and write to RTC */
185 my_puts_P(PSTR("## set_time: Set date failed\n"));
187 my_puts_P(PSTR("## set_time: Get date failed\n"));
190 case 2: /* get date & time */
194 time
= mk_gmtime(&t
);
198 mk_cpm_time(&t
, cpm_time
);
200 my_puts_P(PSTR("## get_time: Get date failed\n"));
205 msg_xmit(3, subf
, sizeof(cpm_time
), cpm_time
);
208 /* ---------------------------------------------------------------------------*/
210 static uint8_t disk_buffer
[CONFIG_CPM_BLOCK_SIZE
];
211 static struct cpm_drive_s drv_table
[CONFIG_CPM_MAX_DRIVE
];
212 static int handle_cpm_drv_to
;
217 for (uint8_t i
= 0; i
< CONFIG_CPM_MAX_DRIVE
; i
++) {
218 struct cpm_drive_s
* p
= &drv_table
[i
];
220 printf_P(PSTR(" dsk%d: %2s %3s attached to %s\n"), i
,
221 p
->opt
&DRV_OPT_RO
? "RO":"RW", p
->opt
&DRV_OPT_DEBUG
? "DBG":"",
228 int drv_detach(uint8_t drv
)
230 if (drv
< CONFIG_CPM_MAX_DRIVE
) {
231 struct cpm_drive_s
*p
= &drv_table
[drv
];
233 debug_cpmsd("## detach dsk%d: %s\n", drv
,
234 p
->img_name
? p
->img_name
: "-");
240 p
->flags
&= ~DRV_FLG_DIRTY
;
247 static int drv_find_file_attached(const char *fn
)
249 for (uint8_t i
= 0; i
< CONFIG_CPM_MAX_DRIVE
; i
++) {
250 struct cpm_drive_s
*p
= &drv_table
[i
];
251 if (p
->img_name
&& !strcmp(fn
, p
->img_name
)) {
258 int drv_attach(uint8_t drv
, const char *filename
, drv_opt_t options
)
262 if (drv
>= CONFIG_CPM_MAX_DRIVE
)
265 struct cpm_drive_s
*p
= &drv_table
[drv
];
267 if (options
& DRV_OPT_REATTATCH
) {
277 if ((p
->opt
^ options
) & DRV_OPT_RO
) {
279 res
= f_open(&p
->fd
, p
->img_name
,
280 FA_READ
| (options
&DRV_OPT_RO
? 0 : FA_WRITE
));
283 p
->opt
= options
& ~DRV_OPT_REATTATCH
;
289 if (drv_find_file_attached(filename
) >= 0)
296 if ((p
->img_name
= strdup(filename
)) == NULL
)
299 res
= f_open(&p
->fd
, p
->img_name
,
300 FA_READ
| (options
&DRV_OPT_RO
? 0 : FA_WRITE
));
302 if (!res
&& f_size(&p
->fd
) < CONFIG_CPM_DISKSIZE
) {
305 debug_cpmsd(" expanding image file from %ld to %ld\n",
306 f_size(&p
->fd
), CONFIG_CPM_DISKSIZE
);
308 res
= f_lseek(&p
->fd
, CONFIG_CPM_DISKSIZE
-CONFIG_CPM_BLOCK_SIZE
);
310 memset(disk_buffer
, 0xe5, CONFIG_CPM_BLOCK_SIZE
);
311 res
= f_write(&p
->fd
, disk_buffer
, CONFIG_CPM_BLOCK_SIZE
, &bw
);
312 if (res
|| bw
< CONFIG_CPM_BLOCK_SIZE
) {
313 debug_cpmsd(" failed! res: %d, bytes written: %u\n", res
, bw
);
315 p
->flags
|= DRV_FLG_DIRTY
;
316 bg_setstat(handle_cpm_drv_to
, 1);
319 debug_cpmsd(" wrong image file zize: %ld, should be %ld\n",
320 f_size(&p
->fd
), CONFIG_CPM_DISKSIZE
);
334 int cpm_drv_to(int state
)
348 if (get_timer(ts
) > 1000) {
349 for (uint_fast8_t i
=0; i
< CONFIG_CPM_MAX_DRIVE
; i
++) {
350 if (drv_table
[i
].flags
& DRV_FLG_DIRTY
) {
351 f_sync(&drv_table
[i
].fd
);
352 drv_table
[i
].flags
&= ~DRV_FLG_DIRTY
;
353 debug_cpmsd("## %7lu f_sync: %c:\n", get_timer(0), i
+ CONFIG_CPM_BASE_DRIVE
);
363 void msg_cpm_result(uint8_t subf
, uint8_t rc
, int res
)
365 uint8_t result_msg
[3];
372 result_msg
[2] = res
>> 8;
375 debug_cpmsd("###%7lu error rc: %.02x, res: %d\n", get_timer(0), rc
, res
);
378 msg_xmit(2, subf
, sizeof(result_msg
), result_msg
);
383 ds 1 ; subcommand (login/read/write)
384 ds 1 ; @adrv (8 bits) +0
385 ds 1 ; @rdrv (8 bits) +1
386 ds 3 ; @xdph (24 bits) +2
389 void do_msg_cpm_login(uint8_t subf
, int len
, uint8_t * msg
)
392 struct cpm_drive_s
*dp
;
398 return msg_cpm_result(subf
, 0x01, res
);
401 debug_cpmsd("\n## %7lu login: %c:\n", get_timer(0), msg
[1]+CONFIG_CPM_BASE_DRIVE
);
403 /* Get relative drive number */
405 if ( drv
>= CONFIG_CPM_MAX_DRIVE
) {
406 debug_cpmsd("## failed: invalid relative drive number '%d'\n", drv
);
407 return msg_cpm_result(subf
, 0x02, res
);
410 dp
= &drv_table
[drv
];
411 dp
->flags
&= ~DRV_FLG_OPEN
;
412 dp
->dph
= ((uint32_t)msg
[4] << 16) + ((uint16_t)msg
[3] << 8) + msg
[2];
415 if (dp
->img_name
== NULL
) {
416 /* no file attached */
417 debug_cpmsd("## failed: no file attached:\n");
418 return msg_cpm_result(subf
, 0x03, res
);
421 dp
->flags
|= DRV_FLG_OPEN
;
424 msg_cpm_result(subf
, 0x00, res
);
430 ds 1 ; subcommand (login/read/write)
431 ds 1 ; @adrv (8 bits) +0
432 ds 1 ; @rdrv (8 bits) +1
433 ds 2 ; @trk (16 bits) +2
434 ds 2 ; @sect(16 bits) +4
435 ds 1 ; @cnt (8 bits) +6
436 ds 3 ; phys. transfer addr +7
446 void do_msg_cpm_rw(uint8_t subf
, int len
, uint8_t * msg
)
449 struct cpm_drive_s
*dp
;
459 return msg_cpm_result(subf
, 0x01, res
);
463 if ( drv
>= CONFIG_CPM_MAX_DRIVE
) {
464 return msg_cpm_result(subf
, 0x02, res
);
467 dp
= &drv_table
[drv
];
469 if (dp
->img_name
== NULL
) {
471 return msg_cpm_result(subf
, 0x06, res
);
476 if (dp
->opt
& DRV_OPT_RO
) {
477 return msg_cpm_result(subf
, 0x05, res
);
484 addr
= ((uint32_t)msg
[ADDR
+2] << 16) + ((uint16_t)msg
[ADDR
+1] << 8) + msg
[ADDR
];
487 /* TODO: tracks per sector from dpb */
488 pos
= (((uint16_t)(msg
[TRK
+1] << 8) + msg
[TRK
]) * 8
489 + ((uint32_t)(msg
[SEC
+1] << 8) + msg
[SEC
])) * CONFIG_CPM_BLOCK_SIZE
;
491 debug_cpmsd("## %7lu cpm_rw: %s %c: trk:%4d, sec: %d, pos: %.8lx, secs: %2d, "
492 "addr: %.5lx\n", get_timer(0), dowrite
? "write" : " read",
493 msg
[RDRV
]+CONFIG_CPM_BASE_DRIVE
,
494 ((uint16_t)(msg
[TRK
+1] << 8) + msg
[TRK
]), msg
[SEC
], pos
, msg
[CNT
], addr
);
496 if (pos
+ secs
* CONFIG_CPM_BLOCK_SIZE
> CONFIG_CPM_DISKSIZE
) {
497 debug_cpmsd(" access > DISKSIZE (%.8lx > %.8lx) aborted!\n",
498 pos
+ secs
* CONFIG_CPM_BLOCK_SIZE
, CONFIG_CPM_DISKSIZE
);
499 return msg_cpm_result(subf
, 0x04, res
);
502 res
= f_lseek(&dp
->fd
, pos
);
504 while (!res
&& secs
--) {
507 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
511 z80_read_block(disk_buffer
, addr
, CONFIG_CPM_BLOCK_SIZE
);
512 z80_bus_cmd(Release
);
514 res
= f_write(&dp
->fd
, disk_buffer
, CONFIG_CPM_BLOCK_SIZE
, &brw
);
516 res
= f_read(&dp
->fd
, disk_buffer
, CONFIG_CPM_BLOCK_SIZE
, &brw
);
518 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
522 z80_write_block(disk_buffer
, addr
, CONFIG_CPM_BLOCK_SIZE
);
523 z80_bus_cmd(Release
);
527 if (brw
!= CONFIG_CPM_BLOCK_SIZE
) {
528 debug_cpmsd(" short read or write: res: %d, bytes rd/wr: %u\n", res
, brw
);
531 addr
+= CONFIG_CPM_BLOCK_SIZE
;
534 if (dowrite
&& !res
) {
535 dp
->flags
|= DRV_FLG_DIRTY
;
536 bg_setstat(handle_cpm_drv_to
, 1);
540 debug_cpmsd("Bus timeout\n");
545 msg_cpm_result(subf
, rc
, res
);
549 const FLASH
struct msg_item z80_messages
[] =
552 1, 3, /* sub fct nr. from, to */
570 2, 3, /* 2: get, 3: set time and date */
571 do_msg_get_set_time
},
572 { 0xff, /* end mark */
581 void do_message(int len
, uint8_t *msg
)
583 uint8_t fct
, sub_fct
;
591 while (fct
!= z80_messages
[i
].fct
) {
592 if (z80_messages
[i
].fct
== 0xff) {
593 DBG_P(1, "do_message: Unknown function: %i, %i\n",
595 return; /* TODO: unknown message # */
601 while (fct
== z80_messages
[i
].fct
) {
602 if (sub_fct
>= z80_messages
[i
].sub_min
&&
603 sub_fct
<= z80_messages
[i
].sub_max
)
608 if (z80_messages
[i
].fct
!= fct
) {
609 DBG_P(1, "do_message: Unknown sub function: %i, %i\n",
611 return; /* TODO: unknown message sub# */
614 (z80_messages
[i
].func
)(sub_fct
, len
, msg
);
619 DBG_P(1, "do_message: to few arguments (%i); this shouldn't happen!\n", len
);
625 #define CTRBUF_LEN 256
627 void check_msg_fifo(void)
630 static int_fast8_t state
;
631 static int msglen
,idx
;
632 static uint8_t buffer
[CTRBUF_LEN
];
634 while ((ch
= z80_memfifo_getc(fifo_msgin
)) >= 0) {
636 case 0: /* wait for start of message */
637 if (ch
== 0xAE) { /* TODO: magic number */
643 case 1: /* get msg len */
644 if (ch
> 0 && ch
<= CTRBUF_LEN
) {
650 case 2: /* get message */
653 do_message(msglen
, buffer
);
662 int msg_handling(int state
)
666 ATOMIC_BLOCK(ATOMIC_FORCEON
) {
667 pending
= (Stat
& S_MSG_PENDING
) != 0;
668 Stat
&= ~S_MSG_PENDING
;
672 uint8_t init_request
;
673 z80_bus_cmd(Request
);
674 init_request
= z80_read(0x43);
675 z80_bus_cmd(Release
);
676 if ( init_request
!= 0) {
677 /* Get address of fifo 0 */
678 z80_bus_cmd(Request
);
679 uint32_t fifo_addr
= z80_read(0x40) +
680 ((uint16_t) z80_read(0x40+1) << 8) +
681 ((uint32_t) z80_read(0x40+2) << 16);
683 z80_bus_cmd(Release
);
685 if (fifo_addr
!= 0) {
686 z80_memfifo_init(fifo_msgin
, fifo_addr
);
700 static int handle_msg_handling
;
702 void setup_z180_serv(void)
705 handle_msg_handling
= bg_register(msg_handling
, 0);
706 handle_cpm_drv_to
= bg_register(cpm_drv_to
, 0);
709 void restart_z180_serv(void)
711 z80_bus_cmd(Request
);
712 z80_memset(0x40, 0, 4);
713 z80_bus_cmd(Release
);
715 for (int i
= 0; i
< NUM_FIFOS
; i
++)
716 z80_memfifo_init(i
, 0);
717 bg_setstat(handle_msg_handling
, 0);
722 /*--------------------------------------------------------------------------*/
724 const FLASH
uint8_t iniprog
[] = {
726 0xED, 0x39, 0x36, // out0 (rcr),a ;disable DRAM refresh
727 0x3E, 0x30, // ld a,030h
728 0xED, 0x39, 0x32 //out0 (dcntl),a ;0 mem, max i/0 wait states
731 const FLASH
uint8_t sertest
[] = {
733 0xED, 0x39, 0x36, // out0 (rcr),a ;disable DRAM refresh
734 0x3E, 0x30, // ld a,030h
735 0xED, 0x39, 0x32, // out0 (dcntl),a ;0 mem, max i/0 wait states
736 0x3E, 0x80, // ld a,M_MPBT ;no MP, PS=10, DR=16, SS=0
737 0xED, 0x39, 0x03, // out0 (cntlb1),a
738 0x3E, 0x64, // ld a,M_RE + M_TE + M_MOD2 ;
739 0xED, 0x39, 0x01, // out0 (cntla1),a
740 0x3E, 0x00, // ld a,0
741 0xED, 0x39, 0x05, // out0 (stat1),a ;Enable rx interrupts
742 0xED, 0x38, 0x05, //l0:in0 a,(stat1)
743 0xE6, 0x80, // and 80h
744 0x28, 0xF9, // jr z,l0
745 0xED, 0x00, 0x09, // in0 b,(rdr1)
746 0xED, 0x38, 0x05, //l1:in0 a,(stat1)
747 0xE6, 0x02, // and 02h
748 0x28, 0xF9, // jr z,l1
749 0xED, 0x01, 0x07, // out0 (tdr1),b
753 const FLASH
uint8_t test1
[] = {
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 0x21, 0x1E, 0x00, // ld hl,dmclrt ;load DMA registers
759 0x06, 0x08, // ld b,dmct_e-dmclrt
760 0x0E, 0x20, // ld c,sar0l
762 0x3E, 0xC3, // ld a,0c3h ;dst +1, src +1, burst
763 0xED, 0x39, 0x31, // out0 (dmode),a ;
764 0x3E, 0x62, // ld a,062h ;enable dma0,
765 0xED, 0x39, 0x30, //cl_1: out0 (dstat),a ;copy 64k
766 0x18, 0xFB, // jr cl_1 ;
767 0x00, 0x00, //dmclrt: dw 0 ;src (inc)
769 0x00, 0x00, // dw 0 ;dst (inc),
771 0x00, 0x00, // dw 0 ;count (64k)