]>
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 uint32_t time_ms
= (len
>= 4) ? *(uint32_t *) msg
: 0;
119 time_ms
= get_timer(time_ms
);
120 msg_xmit(3, subf
, sizeof(time_ms
), (uint8_t *) &time_ms
);
123 /* ---------------------------------------------------------------------------*/
126 #define BLOCK_SIZE 512
127 #define TPA_BASE 0x10000
128 #define COMMON_BASE 0xC000
138 static uint8_t disk_buffer
[BLOCK_SIZE
];
139 static struct cpm_drive_s drv_table
[MAX_DRIVE
];
140 static int handle_cpm_drv_to
;
142 #define f_dirty(fp) ((fp)->fs->wflag != 0)
145 int cpm_drv_to(int state
)
159 if (get_timer(ts
) > 1000) {
160 for (uint_fast8_t i
=0; i
< MAX_DRIVE
; i
++) {
161 // if (&drv_table[i].fd && f_dirty(&drv_table[i].fd)) {
162 if (drv_table
[i
].dirty
) {
163 f_sync(&drv_table
[i
].fd
);
164 drv_table
[i
].dirty
= false;
165 debug_cpmsd("## %7lu f_sync: %c:\n", get_timer(0), i
+'A');
175 void msg_cpm_result(uint8_t subf
, uint8_t rc
, int res
)
177 uint8_t result_msg
[3];
184 result_msg
[2] = res
>> 8;
187 debug_cpmsd("###%7lu error rc: %.02x, res: %d\n", get_timer(0), rc
, res
);
190 msg_xmit(2, subf
, sizeof(result_msg
), result_msg
);
195 ds 1 ; subcommand (login/read/write)
196 ds 1 ; @adrv (8 bits) +0
197 ds 1 ; @rdrv (8 bits) +1
198 ds 3 ; @xdph (24 bits) +2
201 void do_msg_cpm_login(uint8_t subf
, int len
, uint8_t * msg
)
210 if (len
!= 5) { /* TODO: check adrv, rdrv */
211 return msg_cpm_result(subf
, 0x01, res
);
214 debug_cpmsd("\n## %7lu login: %c:\n", get_timer(0), msg
[0]+'A');
218 if ( drv
>= MAX_DRIVE
) {
219 return msg_cpm_result(subf
, 0x02, res
);
223 uint32_t dph = ((uint32_t)msg[4] << 16) + ((uint16_t)msg[3] << 8) + msg[2];
226 if (drv_table
[drv
].img_name
!= NULL
) {
227 debug_cpmsd("## %7lu close: '%s'\n", get_timer(0), drv_table
[drv
].img_name
);
228 f_close(&drv_table
[drv
].fd
);
229 drv_table
[drv
].dirty
= false;
230 free(drv_table
[drv
].img_name
);
231 drv_table
[drv
].img_name
= NULL
;
234 strcpy_P((char *)disk_buffer
, PSTR("dsk0"));
235 disk_buffer
[3] = msg
[0] + '0';
236 if (((np
= getenv((char*)disk_buffer
)) == NULL
) ||
237 ((drv_table
[drv
].img_name
= strdup(np
)) == NULL
)) {
238 return msg_cpm_result(subf
, 0x03, res
);
242 res
= f_open(&drv_table
[drv
].fd
, drv_table
[drv
].img_name
,
245 debug_cpmsd("## %7lu open: '%s', (env: '%s'), res: %d\n", get_timer(0),
246 drv_table
[drv
].img_name
, disk_buffer
, res
);
249 msg_cpm_result(subf
, 0x00, res
);
255 ds 1 ; subcommand (login/read/write)
256 ds 1 ; @adrv (8 bits) +0
257 ds 1 ; @rdrv (8 bits) +1
258 ds 2 ; @trk (16 bits) +2
259 ds 2 ; @sect(16 bits) +4
260 ds 1 ; @cnt (8 bits) +6
261 ds 3 ; phys. transfer addr +7
271 void do_msg_cpm_rw(uint8_t subf
, int len
, uint8_t * msg
)
277 bool dowrite
= (subf
== 2);
282 if (len
!= 10) { /* TODO: check adrv, rdrv */
283 return msg_cpm_result(subf
, 0x01, res
);
287 if ( drv
>= MAX_DRIVE
) {
288 return msg_cpm_result(subf
, 0x02, res
);
292 addr
= ((uint32_t)msg
[ADDR
+2] << 16) + ((uint16_t)msg
[ADDR
+1] << 8) + msg
[ADDR
];
295 /* TODO: tracks per sector from dpb */
296 pos
= (((uint16_t)(msg
[TRK
+1] << 8) + msg
[TRK
]) * 8
297 + ((uint32_t)(msg
[SEC
+1] << 8) + msg
[SEC
])) * BLOCK_SIZE
;
299 debug_cpmsd("## %7lu cpm_rw: %s %c: trk:%4d, sec: %d, pos: %.8lx, secs: %2d, "
300 "addr: %.5lx\n", get_timer(0), dowrite
? "write" : " read",
301 msg
[ADRV
]+'A', ((uint16_t)(msg
[TRK
+1] << 8) + msg
[TRK
]), msg
[SEC
],
302 pos
, msg
[CNT
], addr
);
304 res
= f_lseek(&drv_table
[drv
].fd
, pos
);
305 while (!res
&& secs
--) {
306 unsigned int cnt
, br
;
308 /* check bank boundary crossing */
310 if (addr
< (TPA_BASE
+ COMMON_BASE
) &&
311 (addr
+ BLOCK_SIZE
) > (TPA_BASE
+ COMMON_BASE
)) {
312 cnt
= (TPA_BASE
+ COMMON_BASE
) - addr
;
316 debug_cpmsd("## %67c addr: %.5lx, cnt: %3d\n", ' ', addr
, cnt
);
317 debug_cpmsd("## %67c addr: %.5lx, cnt: %3d\n", ' ', addr
+cnt
-TPA_BASE
, BLOCK_SIZE
-cnt
);
321 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
326 z80_read_block(disk_buffer
, addr
, cnt
);
327 addr
= addr
+ cnt
- TPA_BASE
;
329 z80_read_block(disk_buffer
+cnt
, addr
, BLOCK_SIZE
- cnt
);
330 z80_bus_cmd(Release
);
332 res
= f_write(&drv_table
[drv
].fd
, disk_buffer
, BLOCK_SIZE
, &br
);
334 res
= f_read(&drv_table
[drv
].fd
, disk_buffer
, BLOCK_SIZE
, &br
);
335 if (res
== FR_OK
&& br
== BLOCK_SIZE
) {
336 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
341 z80_write_block(disk_buffer
, addr
, cnt
);
342 addr
= addr
+ cnt
- TPA_BASE
;
344 z80_write_block(disk_buffer
+cnt
, addr
, BLOCK_SIZE
- cnt
);
345 z80_bus_cmd(Release
);
350 if (br
!= BLOCK_SIZE
) {
351 debug_cpmsd("## %7lu f_read res: %d, bytes rd/wr: %u\n", get_timer(0), res
, br
);
352 dump_ram(disk_buffer
, 0, 64, "Read Data");
359 if (dowrite
&& !res
) {
360 // res = f_sync(&drv_table[drv].fd);
361 drv_table
[drv
].dirty
= true;
362 bg_setstat(handle_cpm_drv_to
, 1);
367 debug_cpmsd("Bus timeout\n");
372 msg_cpm_result(subf
, rc
, res
);
376 const FLASH
struct msg_item z80_messages
[] =
379 1, 3, /* sub fct nr. from, to */
396 { 0xff, /* end mark */
405 void do_message(int len
, uint8_t *msg
)
407 uint8_t fct
, sub_fct
;
415 while (fct
!= z80_messages
[i
].fct
) {
416 if (z80_messages
[i
].fct
== 0xff) {
417 DBG_P(1, "do_message: Unknown function: %i, %i\n",
419 return; /* TODO: unknown message # */
425 while (fct
== z80_messages
[i
].fct
) {
426 if (sub_fct
>= z80_messages
[i
].sub_min
&&
427 sub_fct
<= z80_messages
[i
].sub_max
)
432 if (z80_messages
[i
].fct
!= fct
) {
433 DBG_P(1, "do_message: Unknown sub function: %i, %i\n",
435 return; /* TODO: unknown message sub# */
438 (z80_messages
[i
].func
)(sub_fct
, len
, msg
);
443 DBG_P(1, "do_message: to few arguments (%i); this shouldn't happen!\n", len
);
449 #define CTRBUF_LEN 256
451 void check_msg_fifo(void)
454 static int_fast8_t state
;
455 static int msglen
,idx
;
456 static uint8_t buffer
[CTRBUF_LEN
];
458 while ((ch
= z80_memfifo_getc(fifo_msgin
)) >= 0) {
460 case 0: /* wait for start of message */
461 if (ch
== 0xAE) { /* TODO: magic number */
467 case 1: /* get msg len */
468 if (ch
> 0 && ch
<= CTRBUF_LEN
) {
474 case 2: /* get message */
477 do_message(msglen
, buffer
);
486 int msg_handling(int state
)
490 ATOMIC_BLOCK(ATOMIC_FORCEON
) {
491 pending
= (Stat
& S_MSG_PENDING
) != 0;
492 Stat
&= ~S_MSG_PENDING
;
495 * TODO: if pending but no message chr --> special condition. ie init,...
500 case 0: /* need init */
501 /* Get address of fifo_list */
502 z80_bus_cmd(Request
);
503 uint32_t fifo_list
= z80_read(0x40) +
504 ((uint16_t) z80_read(0x41) << 8) +
505 ((uint32_t) z80_read(0x42) << 16);
506 z80_bus_cmd(Release
);
507 if (fifo_list
!= 0) {
508 /* Get address of fifo 0 */
509 z80_bus_cmd(Request
);
510 uint32_t fifo_addr
= z80_read(fifo_list
) +
511 ((uint16_t) z80_read(fifo_list
+1) << 8) +
512 ((uint32_t) z80_read(fifo_list
+2) << 16);
513 z80_bus_cmd(Release
);
514 if (fifo_addr
!= 0) {
515 z80_memfifo_init(fifo_msgin
, fifo_addr
);
520 case 1: /* awaiting messages */
530 static int handle_msg_handling
;
532 void setup_z180_serv(void)
535 handle_msg_handling
= bg_register(msg_handling
, 0);
536 handle_cpm_drv_to
= bg_register(cpm_drv_to
, 0);
539 void restart_z180_serv(void)
541 z80_bus_cmd(Request
);
545 z80_bus_cmd(Release
);
547 for (int i
= 0; i
< NUM_FIFOS
; i
++)
548 z80_memfifo_init(i
, 0);
549 bg_setstat(handle_msg_handling
, 0);
553 /*--------------------------------------------------------------------------*/
555 const FLASH
uint8_t iniprog
[] = {
557 0xED, 0x39, 0x36, // out0 (rcr),a ;disable DRAM refresh
558 0x3E, 0x30, // ld a,030h
559 0xED, 0x39, 0x32 //out0 (dcntl),a ;0 mem, max i/0 wait states
562 const FLASH
uint8_t sertest
[] = {
564 0xED, 0x39, 0x36, // out0 (rcr),a ;disable DRAM refresh
565 0x3E, 0x30, // ld a,030h
566 0xED, 0x39, 0x32, // out0 (dcntl),a ;0 mem, max i/0 wait states
567 0x3E, 0x80, // ld a,M_MPBT ;no MP, PS=10, DR=16, SS=0
568 0xED, 0x39, 0x03, // out0 (cntlb1),a
569 0x3E, 0x64, // ld a,M_RE + M_TE + M_MOD2 ;
570 0xED, 0x39, 0x01, // out0 (cntla1),a
571 0x3E, 0x00, // ld a,0
572 0xED, 0x39, 0x05, // out0 (stat1),a ;Enable rx interrupts
573 0xED, 0x38, 0x05, //l0:in0 a,(stat1)
574 0xE6, 0x80, // and 80h
575 0x28, 0xF9, // jr z,l0
576 0xED, 0x00, 0x09, // in0 b,(rdr1)
577 0xED, 0x38, 0x05, //l1:in0 a,(stat1)
578 0xE6, 0x02, // and 02h
579 0x28, 0xF9, // jr z,l1
580 0xED, 0x01, 0x07, // out0 (tdr1),b
584 const FLASH
uint8_t test1
[] = {
586 0xED, 0x39, 0x36, // out0 (rcr),a ;disable DRAM refresh
587 0x3E, 0x30, // ld a,030h
588 0xED, 0x39, 0x32, // out0 (dcntl),a ;0 mem, max i/0 wait states
589 0x21, 0x1E, 0x00, // ld hl,dmclrt ;load DMA registers
590 0x06, 0x08, // ld b,dmct_e-dmclrt
591 0x0E, 0x20, // ld c,sar0l
593 0x3E, 0xC3, // ld a,0c3h ;dst +1, src +1, burst
594 0xED, 0x39, 0x31, // out0 (dmode),a ;
595 0x3E, 0x62, // ld a,062h ;enable dma0,
596 0xED, 0x39, 0x30, //cl_1: out0 (dstat),a ;copy 64k
597 0x18, 0xFB, // jr cl_1 ;
598 0x00, 0x00, //dmclrt: dw 0 ;src (inc)
600 0x00, 0x00, // dw 0 ;dst (inc),
602 0x00, 0x00, // dw 0 ;count (64k)