Rewrite the tools to use bank1 code with c128 rom!
You don’t have to switch to bank0.
That is how I execute commands to IEC with cpm
I’m updating the bios for use with CmdHd 16mb partitions (or DNP files on U2 or sd2iec)
So, doing a dir-with-partitions is handy.
Code:
If you call the following 'doiec' with HL=cmd to send, reply sits in mybuf (eg at end of prog)
doiec:
ld a,(vic$drv) used IEC device
ld de,mybuf-1
ld (de),a
inc de
; copy HL for BC to cmd buffer
ld b,0
ld c,(hl) ; count of bytes (first entry in HL)
inc c ; include len byte
ldir
; set secondary adr
ld a,(mybuf+1)
cp '$'
ld a,15
jr nz,doiec2
xor a
doiec2:
ld (mybuf-2),a ; secondary address 15 or 0
di ; during this IO, CPM IRQ cannot be allowed (mem layout not compatible)
call begin
ld c,0Dh ; Reset Disk
call 5 ; bdos
ld l,0 ; bios65 code 0 = init (sets up IRQ)
ld a,4 ; tell CPM c128 user command to call bios65
call calusr ; execute user command
doniec:
ei
ret
calusr:
push hl
ld hl,(0001)
ld l,5Ah ; HL= bios user fun)
ex (sp),hl ; hl <-> sp
ret ; jump to user fun with input HL
My approach (I realize it looks like a lot of things, but this way we get things under control)
begin:
- command stored in "mybuf", at end of program or so
mybuf-2 = 0 or 15 (15 for cmd channel)
mybuf-1 = IEC device nr
mybuf = nr of chars to store
mybuf+1 ... = string of PETSCII to send (luckily upper case ASCII are good for this)
- the z80 entry-code stored above 1400 (1000h-1400h = VIC color when IO1 mapped in)
Code above 1400:
- keeping ff00, set FF00 with IO1 (7Eh), IO is needed to reach MMU
- disable IRQ (because cpm fdfd is ending up in bank1 instead of shared bank0)
- IN A, (C) with BC= D506 (MMU RCR config register)
- keep orig RCR
- OUT (C),A with A = 5h = 4k low shared
With this, the z80 can see 0-1000 bank0 and 1400-ffff bank1
Using 4k shared low maps in the bank0 memory with z80 active (this is why z80 code at 1400h)
The c128 OS uses the pages 0 and 1 but the disk routines also use a few 0A00h locations.
(you could have the c128 ROM routines write to bank1 0A00h, ROM doesn't care)
- LDIR HL=swapcode to DE=FFD0h (see last code, BC=15)
The swap code at FFD0h is (normally) located in bank0 (shared high) but we need it in bank1
So, lets write the 6502 code to bank1 (sei, lda #$7e, sta $ff00, lda #0, sta $d505, nop, jmp my02 )
Difference = using io1 instead of io0 and jmp to my 6502 instead of default 2600.
The label my02 will be right after the z80 code in bank1 at 14xx. (see below)
put IRQ handler into 0314 0316 0318 (the Fast85 bios points them at 27xx). The z80 can see 03xx now:
- ld hl,irqs (see at end)
- ld (0314h),hl ; irq
- ld (0316h),hl ; brk
- ld (0318h),hl ; nmi
Switch to 6502 'here' without using code at ffe0
- out (c),a with BC=0D505h and A=1 (enable 6502)
- nop
returns here...
- out(c),a with BC=0D506h and A=orig RCR (kept above)
- ld (ff00),a with A=orig bank (kept above)
- end
my02:
lda #4e Bank1 + Kernal + IO
sta ff00
lda #7
clc
jsr Kclose
lda #1 'Ram1'
tax
jst Ksetbnk
lda #7 ; logical file
ldx mybuf-1 ; device nr (value 8,9... just before buffer)
ldy mybuf-2 ; cmd channel (0 or 15)
jst Ksetlfs
lda mybuf ; nr chars
ldx #low(mybuf+1)
ldy #high(mybuf+1)
jsr kSetNam
jsr Kopen ; finally open
; if carry then A =
; 1 = too many open files (we are on top of Cpm Fast85 so it may have 9 open files)
; 2 = file already open (file 7 was closed at start, shouldn't happen)
; 4 = file not found (unlikely for a command)
; 5 = device not present (we definitely want to know this)
; 6 = not input file
; 240
bcs fail
ldx #0
stx mystat ; I set this to mybuf-3 (0=ok)
stx mylen ; this to mybuf-2
stx mylen+1 ; and mybuf-1 (want to be able to collect more than 255 bytes)
; on return, mybuf-3 should be 0
; on return,. mybuf-1 and -2 s len of IEC reply
; update storage location
ldx #low(mybuf)
stx stor+1
ldx #high(mybuf)
stx stor+2
ldx #7 ; logical file
jsr kChkIn ; Chr in from file 7
gloop:
jsr Kreadst
bne geof
jsr kChrin
ldx mylen
stor:
sta mybuf,X ; store byte (mybuf was updated)
inc mylen
bne gloop
inc mylen+1 ; count of reply hi byte
inc stor+2 ; page byte
jmp gloop
fail: ; error result (eg no device, 5)
sta mystat ; mybuf-3
; Done, close and end
geof:
lda #7
jsr kclose
jsr kclrchn ; default IO
lda #0
jsr ksetbnk ; default bank
sei
lda #7e
sta ff00 ; io1, no kernal
lda #$82
stx $CIA1+0D ; DC0D, turn on CIA timB (like exit Fast85)
jmp FFD6 ; code we installed before (we stay in bank1)
;; From Fast8502, the IRQ handling (throw away)
irqs:
lda $CIA1+0d ; DC0D
lda $CIA2+0d ; DD0D
lda #0F
sta VIC+25 ; Vic Irq ctrl
pla ; mmu state
sta $ff00
pla
tay
pla
tax
pla
rti
swapcode:
sei
lda #$7E ; bank1 io
sta $FF00
lda #0
sta $D505
nop
jmp my02