]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/z180-serv.c
2 * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
4 * SPDX-License-Identifier: GPL-2.0+
11 #include <util/atomic.h>
13 #include "background.h"
19 #include "print-utils.h"
20 #include "z180-serv.h"
24 #define DEBUG_CPM_SDIO 0 /* set to 1 to debug */
26 #define debug_cpmsd(fmt, args...) \
27 debug_cond(DEBUG_CPM_SDIO, fmt, ##args)
31 /*--------------------------------------------------------------------------*/
34 uint8_t z80_get_byte(uint32_t adr
)
46 /*--------------------------------------------------------------------------*/
50 uint8_t sub_min
, sub_max
;
51 void (*func
)(uint8_t, int, uint8_t *);
54 uint32_t msg_to_addr(uint8_t *msg
)
70 static int msg_xmit_header(uint8_t func
, uint8_t subf
, int len
)
72 z80_memfifo_putc(fifo_msgout
, 0xAE);
73 z80_memfifo_putc(fifo_msgout
, len
+2);
74 z80_memfifo_putc(fifo_msgout
, func
);
75 z80_memfifo_putc(fifo_msgout
, subf
);
80 int msg_xmit(uint8_t func
, uint8_t subf
, int len
, uint8_t *msg
)
82 msg_xmit_header(func
, subf
, len
);
84 z80_memfifo_putc(fifo_msgout
, *msg
++);
89 void do_msg_ini_memfifo(uint8_t subf
, int len
, uint8_t * msg
)
93 z80_memfifo_init(subf
, msg_to_addr(msg
));
97 void do_msg_char_out(uint8_t subf
, int len
, uint8_t * msg
)
106 void do_msg_echo(uint8_t subf
, int len
, uint8_t * msg
)
111 msg_xmit(1, 3, len
, msg
);
115 void do_msg_get_timer(uint8_t subf
, int len
, uint8_t * msg
)
117 (void)len
; (void)msg
;
119 uint32_t time_ms
= get_timer(0);
121 msg_xmit(3, subf
, sizeof(time_ms
), (uint8_t *) &time_ms
);
124 /* ---------------------------------------------------------------------------*/
127 #define BLOCK_SIZE 512
128 #define TPA_BASE 0x10000
129 #define COMMON_BASE 0xC000
139 static uint8_t disk_buffer
[BLOCK_SIZE
];
140 static struct cpm_drive_s drv_table
[MAX_DRIVE
];
141 static int handle_cpm_drv_to
;
143 #define f_dirty(fp) ((fp)->fs->wflag != 0)
146 int cpm_drv_to(int state
)
160 if (get_timer(ts
) > 1000) {
161 for (uint_fast8_t i
=0; i
< MAX_DRIVE
; i
++) {
162 // if (&drv_table[i].fd && f_dirty(&drv_table[i].fd)) {
163 if (drv_table
[i
].dirty
) {
164 f_sync(&drv_table
[i
].fd
);
165 drv_table
[i
].dirty
= false;
166 debug_cpmsd("## %7lu f_sync: %c:\n", get_timer(0), i
+'A');
176 void msg_cpm_result(uint8_t subf
, uint8_t rc
, int res
)
178 uint8_t result_msg
[3];
185 result_msg
[2] = res
>> 8;
188 debug_cpmsd("###%7lu error rc: %.02x, res: %d\n", get_timer(0), rc
, res
);
191 msg_xmit(2, subf
, sizeof(result_msg
), result_msg
);
196 ds 1 ; subcommand (login/read/write)
197 ds 1 ; @adrv (8 bits) +0
198 ds 1 ; @rdrv (8 bits) +1
199 ds 3 ; @xdph (24 bits) +2
202 void do_msg_cpm_login(uint8_t subf
, int len
, uint8_t * msg
)
211 if (len
!= 5) { /* TODO: check adrv, rdrv */
212 return msg_cpm_result(subf
, 0x01, res
);
215 debug_cpmsd("\n## %7lu login: %c:\n", get_timer(0), msg
[0]+'A');
219 if ( drv
>= MAX_DRIVE
) {
220 return msg_cpm_result(subf
, 0x02, res
);
224 uint32_t dph = ((uint32_t)msg[4] << 16) + ((uint16_t)msg[3] << 8) + msg[2];
227 if (drv_table
[drv
].img_name
!= NULL
) {
228 debug_cpmsd("## %7lu close: '%s'\n", get_timer(0), drv_table
[drv
].img_name
);
229 f_close(&drv_table
[drv
].fd
);
230 drv_table
[drv
].dirty
= false;
231 free(drv_table
[drv
].img_name
);
232 drv_table
[drv
].img_name
= NULL
;
235 strcpy_P((char *)disk_buffer
, PSTR("dsk0"));
236 disk_buffer
[3] = msg
[0] + '0';
237 if (((np
= getenv((char*)disk_buffer
)) == NULL
) ||
238 ((drv_table
[drv
].img_name
= strdup(np
)) == NULL
)) {
239 return msg_cpm_result(subf
, 0x03, res
);
243 res
= f_open(&drv_table
[drv
].fd
, drv_table
[drv
].img_name
,
246 debug_cpmsd("## %7lu open: '%s', (env: '%s'), res: %d\n", get_timer(0),
247 drv_table
[drv
].img_name
, disk_buffer
, res
);
250 msg_cpm_result(subf
, 0x00, res
);
256 ds 1 ; subcommand (login/read/write)
257 ds 1 ; @adrv (8 bits) +0
258 ds 1 ; @rdrv (8 bits) +1
259 ds 2 ; @trk (16 bits) +2
260 ds 2 ; @sect(16 bits) +4
261 ds 1 ; @cnt (8 bits) +6
262 ds 3 ; phys. transfer addr +7
272 void do_msg_cpm_rw(uint8_t subf
, int len
, uint8_t * msg
)
278 bool dowrite
= (subf
== 2);
283 if (len
!= 10) { /* TODO: check adrv, rdrv */
284 return msg_cpm_result(subf
, 0x01, res
);
288 if ( drv
>= MAX_DRIVE
) {
289 return msg_cpm_result(subf
, 0x02, res
);
293 addr
= ((uint32_t)msg
[ADDR
+2] << 16) + ((uint16_t)msg
[ADDR
+1] << 8) + msg
[ADDR
];
296 /* TODO: tracks per sector from dpb */
297 pos
= (((uint16_t)(msg
[TRK
+1] << 8) + msg
[TRK
]) * 8
298 + ((uint32_t)(msg
[SEC
+1] << 8) + msg
[SEC
])) * BLOCK_SIZE
;
300 debug_cpmsd("## %7lu cpm_rw: %s %c: trk:%4d, sec: %d, pos: %.8lx, secs: %2d, "
301 "addr: %.5lx\n", get_timer(0), dowrite
? "write" : " read",
302 msg
[ADRV
]+'A', ((uint16_t)(msg
[TRK
+1] << 8) + msg
[TRK
]), msg
[SEC
],
303 pos
, msg
[CNT
], addr
);
305 res
= f_lseek(&drv_table
[drv
].fd
, pos
);
306 while (!res
&& secs
--) {
307 unsigned int cnt
, br
;
309 /* check bank boundary crossing */
311 if (addr
< (TPA_BASE
+ COMMON_BASE
) &&
312 (addr
+ BLOCK_SIZE
) > (TPA_BASE
+ COMMON_BASE
)) {
313 cnt
= (TPA_BASE
+ COMMON_BASE
) - addr
;
317 debug_cpmsd("## %67c addr: %.5lx, cnt: %3d\n", ' ', addr
, cnt
);
318 debug_cpmsd("## %67c addr: %.5lx, cnt: %3d\n", ' ', addr
+cnt
-TPA_BASE
, BLOCK_SIZE
-cnt
);
322 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
327 z80_read_block(disk_buffer
, addr
, cnt
);
328 addr
= addr
+ cnt
- TPA_BASE
;
330 z80_read_block(disk_buffer
+cnt
, addr
, BLOCK_SIZE
- cnt
);
331 z80_bus_cmd(Release
);
333 res
= f_write(&drv_table
[drv
].fd
, disk_buffer
, BLOCK_SIZE
, &br
);
335 res
= f_read(&drv_table
[drv
].fd
, disk_buffer
, BLOCK_SIZE
, &br
);
336 if (res
== FR_OK
&& br
== BLOCK_SIZE
) {
337 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
342 z80_write_block(disk_buffer
, addr
, cnt
);
343 addr
= addr
+ cnt
- TPA_BASE
;
345 z80_write_block(disk_buffer
+cnt
, addr
, BLOCK_SIZE
- cnt
);
346 z80_bus_cmd(Release
);
351 if (br
!= BLOCK_SIZE
) {
352 debug_cpmsd("## %7lu f_read res: %d, bytes rd/wr: %u\n", get_timer(0), res
, br
);
353 dump_ram(disk_buffer
, 0, 64, "Read Data");
360 if (dowrite
&& !res
) {
361 // res = f_sync(&drv_table[drv].fd);
362 drv_table
[drv
].dirty
= true;
363 bg_setstat(handle_cpm_drv_to
, 1);
368 debug_cpmsd("Bus timeout\n");
373 msg_cpm_result(subf
, rc
, res
);
377 const FLASH
struct msg_item z80_messages
[] =
380 1, 3, /* sub fct nr. from, to */
397 { 0xff, /* end mark */
406 void do_message(int len
, uint8_t *msg
)
408 uint8_t fct
, sub_fct
;
416 while (fct
!= z80_messages
[i
].fct
) {
417 if (z80_messages
[i
].fct
== 0xff) {
418 DBG_P(1, "do_message: Unknown function: %i, %i\n",
420 return; /* TODO: unknown message # */
426 while (fct
== z80_messages
[i
].fct
) {
427 if (sub_fct
>= z80_messages
[i
].sub_min
&&
428 sub_fct
<= z80_messages
[i
].sub_max
)
433 if (z80_messages
[i
].fct
!= fct
) {
434 DBG_P(1, "do_message: Unknown sub function: %i, %i\n",
436 return; /* TODO: unknown message sub# */
439 (z80_messages
[i
].func
)(sub_fct
, len
, msg
);
444 DBG_P(1, "do_message: to few arguments (%i); this shouldn't happen!\n", len
);
450 #define CTRBUF_LEN 256
452 void check_msg_fifo(void)
455 static int_fast8_t state
;
456 static int msglen
,idx
;
457 static uint8_t buffer
[CTRBUF_LEN
];
459 while ((ch
= z80_memfifo_getc(fifo_msgin
)) >= 0) {
461 case 0: /* wait for start of message */
462 if (ch
== 0xAE) { /* TODO: magic number */
468 case 1: /* get msg len */
469 if (ch
> 0 && ch
<= CTRBUF_LEN
) {
475 case 2: /* get message */
478 do_message(msglen
, buffer
);
487 int msg_handling(int state
)
491 ATOMIC_BLOCK(ATOMIC_FORCEON
) {
492 pending
= (Stat
& S_MSG_PENDING
) != 0;
493 Stat
&= ~S_MSG_PENDING
;
498 case 0: /* need init */
499 /* Get address of fifo_list */
500 z80_bus_cmd(Request
);
501 uint32_t fifo_list
= z80_read(0x40) +
502 ((uint16_t) z80_read(0x41) << 8) +
503 ((uint32_t) z80_read(0x42) << 16);
504 z80_bus_cmd(Release
);
505 if (fifo_list
!= 0) {
506 /* Get address of fifo 0 */
507 z80_bus_cmd(Request
);
508 uint32_t fifo_addr
= z80_read(fifo_list
) +
509 ((uint16_t) z80_read(fifo_list
+1) << 8) +
510 ((uint32_t) z80_read(fifo_list
+2) << 16);
511 z80_bus_cmd(Release
);
512 if (fifo_addr
!= 0) {
513 z80_memfifo_init(fifo_msgin
, fifo_addr
);
518 case 1: /* awaiting messages */
528 static int handle_msg_handling
;
530 void setup_z180_serv(void)
533 handle_msg_handling
= bg_register(msg_handling
, 0);
534 handle_cpm_drv_to
= bg_register(cpm_drv_to
, 0);
537 void restart_z180_serv(void)
539 z80_bus_cmd(Request
);
543 z80_bus_cmd(Release
);
545 for (int i
= 0; i
< NUM_FIFOS
; i
++)
546 z80_memfifo_init(i
, 0);
547 bg_setstat(handle_msg_handling
, 0);
551 /*--------------------------------------------------------------------------*/
553 const FLASH
uint8_t iniprog
[] = {
555 0xED, 0x39, 0x36, // out0 (rcr),a ;disable DRAM refresh
556 0x3E, 0x30, // ld a,030h
557 0xED, 0x39, 0x32 //out0 (dcntl),a ;0 mem, max i/0 wait states
560 const FLASH
uint8_t sertest
[] = {
562 0xED, 0x39, 0x36, // out0 (rcr),a ;disable DRAM refresh
563 0x3E, 0x30, // ld a,030h
564 0xED, 0x39, 0x32, // out0 (dcntl),a ;0 mem, max i/0 wait states
565 0x3E, 0x80, // ld a,M_MPBT ;no MP, PS=10, DR=16, SS=0
566 0xED, 0x39, 0x03, // out0 (cntlb1),a
567 0x3E, 0x64, // ld a,M_RE + M_TE + M_MOD2 ;
568 0xED, 0x39, 0x01, // out0 (cntla1),a
569 0x3E, 0x00, // ld a,0
570 0xED, 0x39, 0x05, // out0 (stat1),a ;Enable rx interrupts
571 0xED, 0x38, 0x05, //l0:in0 a,(stat1)
572 0xE6, 0x80, // and 80h
573 0x28, 0xF9, // jr z,l0
574 0xED, 0x00, 0x09, // in0 b,(rdr1)
575 0xED, 0x38, 0x05, //l1:in0 a,(stat1)
576 0xE6, 0x02, // and 02h
577 0x28, 0xF9, // jr z,l1
578 0xED, 0x01, 0x07, // out0 (tdr1),b
582 const FLASH
uint8_t test1
[] = {
584 0xED, 0x39, 0x36, // out0 (rcr),a ;disable DRAM refresh
585 0x3E, 0x30, // ld a,030h
586 0xED, 0x39, 0x32, // out0 (dcntl),a ;0 mem, max i/0 wait states
587 0x21, 0x1E, 0x00, // ld hl,dmclrt ;load DMA registers
588 0x06, 0x08, // ld b,dmct_e-dmclrt
589 0x0E, 0x20, // ld c,sar0l
591 0x3E, 0xC3, // ld a,0c3h ;dst +1, src +1, burst
592 0xED, 0x39, 0x31, // out0 (dmode),a ;
593 0x3E, 0x62, // ld a,062h ;enable dma0,
594 0xED, 0x39, 0x30, //cl_1: out0 (dstat),a ;copy 64k
595 0x18, 0xFB, // jr cl_1 ;
596 0x00, 0x00, //dmclrt: dw 0 ;src (inc)
598 0x00, 0x00, // dw 0 ;dst (inc),
600 0x00, 0x00, // dw 0 ;count (64k)