]>
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 0 /* 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 /* ---------------------------------------------------------------------------*/
211 static uint8_t disk_buffer
[CONFIG_CPM_BLOCK_SIZE
];
212 static struct cpm_drive_s drv_table
[CONFIG_CPM_MAX_DRIVE
];
213 static int handle_cpm_drv_to
;
215 typedef enum {SINGLE
, START
, MIDDLE
, END
} dbgmsg_t
;
217 void drv_debug(dbgmsg_t phase
, const FLASH
char *const fmt
, ...) \
219 struct cpm_drive_s
*dp
= &drv_table
[drv
];
221 if (dp
->opt
& DRV_OPT_DEBUG
) {
226 if (phase
== SINGLE
|| phase
== START
)
227 printf_P(PSTR("# %7lu dsk%d: "), get_timer(0), drv
);
229 vfprintf_P (stdout
, fmt
, ap
);
231 if (phase
== SINGLE
|| phase
== END
)
240 for (uint8_t i
= 0; i
< CONFIG_CPM_MAX_DRIVE
; i
++) {
241 struct cpm_drive_s
* p
= &drv_table
[i
];
243 printf_P(PSTR(" dsk%d: %2s %3s attached to %s\n"), i
,
244 p
->opt
&DRV_OPT_RO
? "RO":"RW", p
->opt
&DRV_OPT_DEBUG
? "DBG":"",
251 int drv_detach(uint8_t unit
)
254 if (drv
< CONFIG_CPM_MAX_DRIVE
) {
255 struct cpm_drive_s
*p
= &drv_table
[drv
];
257 drv_debug(SINGLE
, PSTR("detach from '%s'"), p
->img_name
? p
->img_name
: "-");
263 p
->flags
&= ~DRV_FLG_DIRTY
;
266 uint32_t scb
= getenv_ulong(ENV_CPM3_SCB
, 16, 0);
267 if (scb
&& (z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
268 z80_write(scb
+ 0xf0, 0xff);
269 z80_write(p
->dph
+ 11, 0xff);
270 z80_bus_cmd(Release
);
277 static int drv_find_file_attached(const char *fn
)
279 for (uint8_t i
= 0; i
< CONFIG_CPM_MAX_DRIVE
; i
++) {
280 struct cpm_drive_s
*p
= &drv_table
[i
];
281 if (p
->img_name
&& !strcmp(fn
, p
->img_name
)) {
288 int drv_attach(uint8_t unit
, const char *filename
, drv_opt_t options
)
293 if (drv
>= CONFIG_CPM_MAX_DRIVE
)
296 struct cpm_drive_s
*p
= &drv_table
[drv
];
298 if (options
& DRV_OPT_REATTATCH
) {
308 if ((p
->opt
^ options
) & DRV_OPT_RO
) {
310 res
= f_open(&p
->fd
, p
->img_name
,
311 FA_READ
| (options
&DRV_OPT_RO
? 0 : FA_WRITE
));
314 p
->opt
= options
& ~DRV_OPT_REATTATCH
;
320 if (drv_find_file_attached(filename
) >= 0)
327 if ((p
->img_name
= strdup(filename
)) == NULL
)
330 res
= f_open(&p
->fd
, p
->img_name
,
331 FA_READ
| (options
&DRV_OPT_RO
? 0 : FA_WRITE
));
333 if (!res
&& f_size(&p
->fd
) < CONFIG_CPM_DISKSIZE
) {
336 debug_cpmsd(" expanding image file from %ld to %ld\n",
337 f_size(&p
->fd
), CONFIG_CPM_DISKSIZE
);
339 res
= f_lseek(&p
->fd
, CONFIG_CPM_DISKSIZE
-CONFIG_CPM_BLOCK_SIZE
);
341 memset(disk_buffer
, 0xe5, CONFIG_CPM_BLOCK_SIZE
);
342 res
= f_write(&p
->fd
, disk_buffer
, CONFIG_CPM_BLOCK_SIZE
, &bw
);
343 if (res
|| bw
< CONFIG_CPM_BLOCK_SIZE
) {
344 debug_cpmsd(" failed! res: %d, bytes written: %u\n", res
, bw
);
346 p
->flags
|= DRV_FLG_DIRTY
;
347 bg_setstat(handle_cpm_drv_to
, 1);
350 drv_debug(SINGLE
, PSTR("wrong image file size: %ld, should be %ld"),
351 f_size(&p
->fd
), CONFIG_CPM_DISKSIZE
);
365 int cpm_drv_to(int state
)
379 if (get_timer(ts
) > 1000) {
380 for (uint_fast8_t i
=0; i
< CONFIG_CPM_MAX_DRIVE
; i
++) {
381 if (drv_table
[i
].flags
& DRV_FLG_DIRTY
) {
382 drv_table
[i
].flags
&= ~DRV_FLG_DIRTY
;
383 f_sync(&drv_table
[i
].fd
);
385 drv_debug(SINGLE
, PSTR("f_sync"));
394 static const FLASH
char * const FLASH rc_messages
[] = {
396 FSTR("Internal error: wrong message len"), /* 01 */
397 FSTR("Invalid relative drive #"), /* 02 */
398 FSTR("Bus timeout"), /* 03 */
399 FSTR("Access byond disk size"), /* 04 */
400 FSTR("Write protect"), /* 05 */
401 FSTR("No media"), /* 06 */
402 FSTR("R/W address == 0 !!!!"), /* 07 */
405 void msg_cpm_result(uint8_t subf
, uint8_t rc
, int res
)
407 uint8_t result_msg
[3];
414 result_msg
[2] = res
>> 8;
416 msg_xmit(2, subf
, sizeof(result_msg
), result_msg
);
419 drv_debug(END
, PSTR(" rc: %.02x/%d, '%S'"),
420 rc
, res
, rc_messages
[rc
& 0x7f]);
422 drv_debug(END
, PSTR(""));
428 ds 1 ; subcommand (login/read/write)
429 ds 1 ; @adrv (8 bits) +0
430 ds 1 ; @rdrv (8 bits) +1
431 ds 3 ; @xdph (24 bits) +2
434 void do_msg_cpm_login(uint8_t subf
, int len
, uint8_t * msg
)
436 struct cpm_drive_s
*dp
;
441 /* Get relative drive number */
443 drv_debug(START
, PSTR("login"));
446 return msg_cpm_result(subf
, 0x01, res
);
449 if ( drv
>= CONFIG_CPM_MAX_DRIVE
) {
450 /* invalid relative drive number */
451 return msg_cpm_result(subf
, 0x02, res
);
454 dp
= &drv_table
[drv
];
455 dp
->flags
&= ~DRV_FLG_OPEN
;
456 dp
->dph
= ((uint32_t)msg
[4] << 16) + ((uint16_t)msg
[3] << 8) + msg
[2];
458 if (dp
->img_name
== NULL
) {
459 /* no file attached */
460 return msg_cpm_result(subf
, 0x06, res
);
464 res
= f_open(&dp
->fd
, dp
->img_name
,
465 FA_READ
| (dp
->opt
&DRV_OPT_RO
? 0 : FA_WRITE
));
467 dp
->flags
|= DRV_FLG_OPEN
;
470 msg_cpm_result(subf
, 0x00, res
);
476 ds 1 ; subcommand (login/read/write)
477 ds 1 ; @adrv (8 bits) +0
478 ds 1 ; @rdrv (8 bits) +1
479 ds 2 ; @trk (16 bits) +2
480 ds 2 ; @sect(16 bits) +4
481 ds 1 ; @cnt (8 bits) +6
482 ds 3 ; phys. transfer addr +7
492 void do_msg_cpm_rw(uint8_t subf
, int len
, uint8_t * msg
)
494 struct cpm_drive_s
*dp
;
506 dowrite
= (subf
== 2);
508 drv_debug(START
, PSTR("%2S"), dowrite
? PSTR("W ") : PSTR(" R"));
511 return msg_cpm_result(subf
, 0x01, res
);
513 if ( drv
>= CONFIG_CPM_MAX_DRIVE
) {
514 return msg_cpm_result(subf
, 0x02, res
);
517 dp
= &drv_table
[drv
];
518 track
= (uint16_t)(msg
[TRK
+1] << 8) + msg
[TRK
];
519 sec
= (uint16_t)(msg
[SEC
+1] << 8) + msg
[SEC
];
521 addr
= ((uint32_t)msg
[ADDR
+2] << 16) + ((uint16_t)msg
[ADDR
+1] << 8) + msg
[ADDR
];
523 if (dp
->img_name
== NULL
) {
525 return msg_cpm_result(subf
, 0x06, res
);
528 /* TODO: tracks per sector from dpb */
529 pos
= (track
* 8UL + sec
) * CONFIG_CPM_BLOCK_SIZE
;
531 drv_debug(MIDDLE
, PSTR(" T:%4d, S:%2d, cnt:%2d, lba: %.8lx, addr: %.5lx"),
532 track
, sec
, secs
, pos
, addr
);
535 return msg_cpm_result(subf
, 0x07, res
);
538 if (dowrite
&& dp
->opt
& DRV_OPT_RO
) {
539 return msg_cpm_result(subf
, 0x05, res
);
543 if (pos
+ secs
* CONFIG_CPM_BLOCK_SIZE
> CONFIG_CPM_DISKSIZE
) {
544 drv_debug(MIDDLE
, PSTR(" access > DISKSIZE:%.8lx!"),
545 CONFIG_CPM_DISKSIZE
);
546 return msg_cpm_result(subf
, 0x04, res
);
549 res
= f_lseek(&dp
->fd
, pos
);
551 while (!res
&& secs
--) {
554 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
558 z80_read_block(disk_buffer
, addr
, CONFIG_CPM_BLOCK_SIZE
);
559 z80_bus_cmd(Release
);
561 res
= f_write(&dp
->fd
, disk_buffer
, CONFIG_CPM_BLOCK_SIZE
, &brw
);
563 res
= f_read(&dp
->fd
, disk_buffer
, CONFIG_CPM_BLOCK_SIZE
, &brw
);
565 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
569 z80_write_block(disk_buffer
, addr
, CONFIG_CPM_BLOCK_SIZE
);
570 z80_bus_cmd(Release
);
574 if (brw
!= CONFIG_CPM_BLOCK_SIZE
) {
575 drv_debug(MIDDLE
, PSTR(" short rd/wr: res: %d, brw: %u"),
579 addr
+= CONFIG_CPM_BLOCK_SIZE
;
582 if (dowrite
&& !res
) {
583 dp
->flags
|= DRV_FLG_DIRTY
;
584 bg_setstat(handle_cpm_drv_to
, 1);
588 /* Bus timeout. how can this happen? */
593 msg_cpm_result(subf
, rc
, res
);
597 const FLASH
struct msg_item z80_messages
[] =
600 1, 3, /* sub fct nr. from, to */
618 2, 3, /* 2: get, 3: set time and date */
619 do_msg_get_set_time
},
620 { 0xff, /* end mark */
629 void do_message(int len
, uint8_t *msg
)
631 uint8_t fct
, sub_fct
;
639 while (fct
!= z80_messages
[i
].fct
) {
640 if (z80_messages
[i
].fct
== 0xff) {
641 DBG_P(1, "do_message: Unknown function: %i, %i\n",
643 return; /* TODO: unknown message # */
649 while (fct
== z80_messages
[i
].fct
) {
650 if (sub_fct
>= z80_messages
[i
].sub_min
&&
651 sub_fct
<= z80_messages
[i
].sub_max
)
656 if (z80_messages
[i
].fct
!= fct
) {
657 DBG_P(1, "do_message: Unknown sub function: %i, %i\n",
659 return; /* TODO: unknown message sub# */
662 (z80_messages
[i
].func
)(sub_fct
, len
, msg
);
667 DBG_P(1, "do_message: to few arguments (%i); this shouldn't happen!\n", len
);
673 #define CTRBUF_LEN 256
675 void check_msg_fifo(void)
678 static int_fast8_t state
;
679 static int msglen
,idx
;
680 static uint8_t buffer
[CTRBUF_LEN
];
682 while ((ch
= z80_memfifo_getc(fifo_msgin
)) >= 0) {
684 case 0: /* wait for start of message */
685 if (ch
== 0xAE) { /* TODO: magic number */
691 case 1: /* get msg len */
692 if (ch
> 0 && ch
<= CTRBUF_LEN
) {
698 case 2: /* get message */
701 do_message(msglen
, buffer
);
710 int msg_handling(int state
)
714 ATOMIC_BLOCK(ATOMIC_FORCEON
) {
715 pending
= (Stat
& S_MSG_PENDING
) != 0;
716 Stat
&= ~S_MSG_PENDING
;
720 uint8_t init_request
;
721 z80_bus_cmd(Request
);
722 init_request
= z80_read(0x43);
723 z80_bus_cmd(Release
);
724 if ( init_request
!= 0) {
725 /* Get address of fifo 0 */
726 z80_bus_cmd(Request
);
727 uint32_t fifo_addr
= z80_read(0x40) +
728 ((uint16_t) z80_read(0x40+1) << 8) +
729 ((uint32_t) z80_read(0x40+2) << 16);
731 z80_bus_cmd(Release
);
733 if (fifo_addr
!= 0) {
734 z80_memfifo_init(fifo_msgin
, fifo_addr
);
748 static int handle_msg_handling
;
750 void setup_z180_serv(void)
753 handle_msg_handling
= bg_register(msg_handling
, 0);
754 handle_cpm_drv_to
= bg_register(cpm_drv_to
, 0);
757 void restart_z180_serv(void)
759 z80_bus_cmd(Request
);
760 z80_memset(0x40, 0, 4);
761 z80_bus_cmd(Release
);
763 for (int i
= 0; i
< NUM_FIFOS
; i
++)
764 z80_memfifo_init(i
, 0);
765 bg_setstat(handle_msg_handling
, 0);
770 /*--------------------------------------------------------------------------*/
772 const FLASH
uint8_t iniprog
[] = {
774 0xED, 0x39, 0x36, // out0 (rcr),a ;disable DRAM refresh
775 0x3E, 0x30, // ld a,030h
776 0xED, 0x39, 0x32 //out0 (dcntl),a ;0 mem, max i/0 wait states
779 const FLASH
uint8_t sertest
[] = {
781 0xED, 0x39, 0x36, // out0 (rcr),a ;disable DRAM refresh
782 0x3E, 0x30, // ld a,030h
783 0xED, 0x39, 0x32, // out0 (dcntl),a ;0 mem, max i/0 wait states
784 0x3E, 0x80, // ld a,M_MPBT ;no MP, PS=10, DR=16, SS=0
785 0xED, 0x39, 0x03, // out0 (cntlb1),a
786 0x3E, 0x64, // ld a,M_RE + M_TE + M_MOD2 ;
787 0xED, 0x39, 0x01, // out0 (cntla1),a
788 0x3E, 0x00, // ld a,0
789 0xED, 0x39, 0x05, // out0 (stat1),a ;Enable rx interrupts
790 0xED, 0x38, 0x05, //l0:in0 a,(stat1)
791 0xE6, 0x80, // and 80h
792 0x28, 0xF9, // jr z,l0
793 0xED, 0x00, 0x09, // in0 b,(rdr1)
794 0xED, 0x38, 0x05, //l1:in0 a,(stat1)
795 0xE6, 0x02, // and 02h
796 0x28, 0xF9, // jr z,l1
797 0xED, 0x01, 0x07, // out0 (tdr1),b
801 const FLASH
uint8_t test1
[] = {
803 0xED, 0x39, 0x36, // out0 (rcr),a ;disable DRAM refresh
804 0x3E, 0x30, // ld a,030h
805 0xED, 0x39, 0x32, // out0 (dcntl),a ;0 mem, max i/0 wait states
806 0x21, 0x1E, 0x00, // ld hl,dmclrt ;load DMA registers
807 0x06, 0x08, // ld b,dmct_e-dmclrt
808 0x0E, 0x20, // ld c,sar0l
810 0x3E, 0xC3, // ld a,0c3h ;dst +1, src +1, burst
811 0xED, 0x39, 0x31, // out0 (dmode),a ;
812 0x3E, 0x62, // ld a,062h ;enable dma0,
813 0xED, 0x39, 0x30, //cl_1: out0 (dstat),a ;copy 64k
814 0x18, 0xFB, // jr cl_1 ;
815 0x00, 0x00, //dmclrt: dw 0 ;src (inc)
817 0x00, 0x00, // dw 0 ;dst (inc),
819 0x00, 0x00, // dw 0 ;count (64k)