;Diskmon.s Abacus Amiga Disk Drives Inside and Out
;Assemble with AssemPro Amiga, make sure Optmize BAckward Bcc's is OFF
Exodus:
OldOpenLibrary       =-408
CloseLibrary         =-414
AllocMem             =-198
FreeMem              =-210
Read                 = -42
Write                = -48
Open                 = -30
Close                = -36
FindTask             =-294
OpenDevice           =-444
CloseDevice          =-450
DoIO                 =-456

        tst.l d0                ;parameter length
        beq.s run
        cmp.l #5,d0
        bne.s run
        cmp.b #"d",(a0)         ;test for "dfx:"
        bne.s run
        cmp.b #"f",1(a0)
        bne.s run
        cmp.b #":",3(a0)
        bne.s run
        move.b 2(a0),d0
        sub.b #"0",d0
        move.b d0,device        ;SET DRIVE
run:    move.l 4,a6             ;dos.library open
        lea dosname,a1
        jsr OldOpenLibrary(a6)
        move.l d0,dosbase
        beq error
        move.l #$10002,d1       ;512 byte buffer in chipmem
        move.l #512,d0          ;reserved
        jsr AllocMem(a6)
        move.l d0,buffer
        beq error
        sub.l a1,a1             ;task for trackdisk.device
        jsr FindTask(a6)
        lea diskport,a0
        move.l d0,16(a0)
        clr.l d0
        move.b device,d0        ;trackdisk.device for dfx: open
        moveq #0,d1
        lea diskioreq,a1
        lea trkdisk,a0
        jsr OpenDevice(a6)
        tst.l d0
        bne nodrive             ;close library,release memory
        move.b device,d0        ;ldrive in command line set
        add.b #"0",d0
        move.b d0,drive
        move.l dosbase,a6       ;open raw-window
        move.l #title,d1
        move.l #1005,d2
        jsr Open(a6)
        move.l d0,wdhd          ;windowhandle
        beq error
        jsr crsroff             ;cursor off
        move.l wdhd,d1          ;menu display
        move.l #top,d2
        move.l #toplen,d3
        jsr Write(a6)

        jsr dumpblock           ;output block # and $,clr errors
        move.b #"r",key         ;simlulate read command
        bra.s start

main:   jsr dumptype            ;typ output
        jsr dumpcheck           ;checksum output
        jsr getkey              ;get next key
start:  cmp.b #$1b,key          ;program end
        beq.s quit
        cmp.b #"r",key          ;read block and display
        beq readsec        cmp.b #"w",key          ;write blockand display
        beq writesec        cmp.b #"c",key          ;data checksum create
        beq check        cmp.b #"#",key          ;decimal block input and read
        beq blockedit        cmp.b #"$",key          ;hexadecimal block input and read
        beq blockedithex        cmp.b #"+",key          ;block +1 read
        beq up        cmp.b #"-",key          ;block -1 read
        beq down        cmp.b #"a",key          ;ascii input

        beq asciiedit        cmp.b #"h",key          ;hex input
        beq hexedit        bne main
quit:   move.l dosbase,a6       ;close window
        move.l wdhd,d1
        jsr Close(a6)

        move.l 4,a6             ;close trackdisk.device
        lea diskioreq,a1
        jsr CloseDevice(a6)

nodrive:move.l buffer,a1        ;free buffer
        move.l #512,d0
        jsr FreeMem(a6)

        move.l dosbase,a1       ;close dos.library
        jsr CloseLibrary(a6)
        clr.l d0
        rts

error:  moveq #100,d0           ;returncode 100 for system error
        rts

dumphex:cmp.b #"a",key          ;ascii input- NTSC added
        beq hexstop             ;NTSC added
        move.b #"0",row         ;cursor pos.
        move.b #"6",row+1
        move.b #"0",col
        move.b #"2",col+1
        move.l buffer,buffptr   ;buffer pointer to start
        clr.w adr
        moveq #15,d6            ;16 lines
poshex: jsr cursor              ;address output
        move.w adr,d0
        jsr convword
        jsr printword
        move.b #" ",key         ;space printed
        jsr printkey
        add.w #$20,adr          ;inc. address by $20
        moveq #15,d5            ;16 words per line
        lea linebuf,a2          ;buffer for line
x:      move.l buffptr,a1
        add.l #2,buffptr        ;buffer pointer2
        move.w (a1),d0          ;get word
        jsr convword            ;convert to ascii
        move.l mytext,(a2)+     ;copy to line buffer
        dbra d5,x
        jsr printline
        move.b row+1,d0         ;cursor pos. line +1
        cmp.b #"9",d0
        bne.s l1
        add.b #1,row
        move.b #"0"-1,row+1
l1:     add.b #1,row+1
        dbra d6,poshex
hexstop: rts

convdez:lea mytext,a0           ;convert word in d0 by 4
        divu #1000,d0           ;decimal number
        add.b #"0",d0
        move.b d0,(a0)+
        clr.w d0
        swap d0
        divu #100,d0
        add.b #"0",d0
        move.b d0,(a0)+
        clr.w d0
        swap d0
        divu #10,d0
        add.b #"0",d0
        move.b d0,(a0)+
        clr.w d0
        swap d0
        add.b #"0",d0
        move.b d0,(a0)+
        rts

convword:                       ;convert word in d0 to ascii text
        moveq #3,d2
        lea mytext+4,a0
l0:     move.b d0,d1
        and.b #$0f,d1
        lsr.w #4,d0
        cmp.b #$09,d1
        bgt.s hex
        add.b #"0",d1
        bra.s do
hex:    add.b #"a"-10,d1
do:     move.b d1,-(a0)
        dbra d2,l0
        rts

printword:                      ;text output
        move.l wdhd,d1
        move.l #mytext,d2
        moveq #4,d3
        jsr Write(a6)
        rts

dumpasc:cmp.b #"h",key          ;check for hex display NTSC only
        beq ascstop
        move.b #"0",row         ;block ascii output Pal=2
        move.b #"6",row+1       ;PAl =3
        move.b #"0",col         ;PAL =0
        move.b #"2",col+1       ;Pal =2
        move.l buffer,buffptr   ;
        clr.w adr               ;
        moveq #7,d6             ; PAL=7
posasc: jsr cursor              ;address output
        move.w adr,d0
        jsr convword
        jsr printword
        move.b #" ",key         ;space printed
        jsr printkey
        add.w #$40,adr
        moveq #63,d5
        lea linebuf,a2          ;line buffer
        move.l buffptr,a1
y:      move.b (a1)+,d0         ;get byte and mask ascii
        cmp.b #" ",d0
        blt.s dot
        cmp.b #"z",d0
        bgt.s dot
        move.b d0,(a2)+
        bra.s chr
dot:    move.b #".",(a2)+       ;replace control char with "."
chr:    dbra d5,y
        move.l a1,buffptr
        jsr printline           ;line buffer output
        move.b row+1,d0         ;cursor pos. line +1
        cmp.b #"9",d0
        bne.s l2
        add.b #1,row
        move.b #"0"-1,row+1
l2:     add.b #1,row+1
        dbra d6,posasc
        move.l wdhd,d1             ;NTSC only to clear 8 hex lines
        move.l #clrhex,d2          ;NTSC only
        move.l #clrhexlen,d3       ;NTSC only
        jsr Write(a6)              ;NTSC only


ascstop:rts

printline:                      ;line buffer output
        move.l wdhd,d1
        move.l #linebuf,d2
        moveq #64,d3
        jsr Write(a6)
        rts

dumpcheck:                      ;checksum output
        move.b #"0",row
        move.b #"4",row+1
        move.b #"5",col
        move.b #"1",col+1
        jsr cursor
        move.l buffer,a0        ;upper word
        move.w 20(a0),d0
        jsr convword
        jsr printword
        move.l buffer,a0        ;lower word
        move.w 22(a0),d0
        jsr convword
        jsr printword
        rts

dumpblock:                      ;block number dez. and hex. output
        move.b #"0",row
        move.b #"4",row+1
        move.b #"0",col
        move.b #"9",col+1
        jsr cursor
        move.w block,d0         ;set offset for read/write
        mulu #512,d0
        move.l d0,offset
        clr.l d0
        move.w block,d0
        jsr convdez             ;convert block decimal
        jsr printword
        move.b #"0",row         ;convert block hex.
        move.b #"4",row+1
        move.b #"1",col
        move.b #"5",col+1
        jsr cursor
        move.w block,d0
        jsr convword
        jsr printword

        move.l #clear,d4        ;clr error message
        moveq #clrlen,d5
        jsr doerr
        rts

dumptype:
        move.b #"0",row         ;block type output
        move.b #"2",row+1
        move.b #"0",col
        move.b #"2",col+1
        jsr cursor
        move.l wdhd,d1
        move.l #unkn,d2         ;typ unknown
        moveq #10,d3            ;typ-lenght
        cmp.w #2,block          ;boot       block=0,1
        bge.s noboot
        move.l #boot,d2
        bra.s noknown
noboot: move.l buffer,a0
        cmp.l #8,(a0)           ;data       1.LW=$00000008
        bne.s nodata
        move.l #dat,d2
nodata: cmp.l #$10,(a0)         ;filelist   1.LW=$00000010
        bne.s nolisting
        cmp.l #-3,508(a0)       ;filelist 127.LW=$fffffffd
        bne.s nolisting
        move.l #flist,d2
nolisting: cmp.l #2,(a0)        ;root,userdir,filehead
                                ;           1.LW=$00000002
        bne.s noknown
        cmp.l #1,508(a0)        ;root     127.LW=$00000001
        bne.s noroot
        move.l #root,d2
noroot: cmp.l #2,508(a0)        ;userdir  127.LW=$00000002
        bne.s noudir
        move.l #udir,d2
noudir: cmp.l #-3,508(a0)       ;filehead 127.LW=$fffffffd
        bne.s noknown
        move.l #fhead,d2
noknown:jsr Write(a6)
        rts

getkey: move.l wdhd,d1          ;wait for key
        move.l #key,d2          ;get next key
        moveq #1,d3
        jsr Read(a6)
        rts

printkey:                       ;char key printed
        move.l wdhd,d1
        move.l #key,d2
        moveq #1,d3
        jsr Write(a6)
        rts

cursor: jsr cursoff             ;cursor off, no status change
        move.l wdhd,d1          ;cursor on #address position
        move.l #adrpos,d2
        moveq #7,d3
        jsr Write(a6)
        lea mytext,a0
        move.l #"    ",(a0)
        btst #0,crsrstatus      ;when cursor off,clr address
        beq.s noadr
        move.l buffptr,d0       ;else output address
        sub.l buffer,d0         ;address=pointer-start
        jsr convdez
noadr:  jsr printword
        move.l wdhd,d1          ;position cursor on $address
        move.l #adrpos2,d2
        moveq #7,d3
        jsr Write(a6)
        lea mytext,a0
        move.l #"    ",(a0)
        btst #0,crsrstatus      ;when cursor off, clr address
        beq.s noadr2
        move.l buffptr,d0       ;else output address
        sub.l buffer,d0
        jsr convword
noadr2: jsr printword
        move.l wdhd,d1          ;cursor position
        move.l #pos,d2
        moveq #7,d3
        jsr Write(a6)
        btst #0,crsrstatus
        beq.s no
        jsr curson              ;cursor on. no status change
no:     rts

crsron: bset #0,crsrstatus      ;switch cursor on
curson: move.l wdhd,d1
        move.l #con,d2
        moveq #3,d3
        jsr Write(a6)
        rts

crsroff:bclr #0,crsrstatus      ;switch cursor off
cursoff:move.l wdhd,d1
        move.l #coff,d2
        moveq #4,d3
        jsr Write(a6)
        rts

doerr:  move.l dosbase,a6       ;output error message d4/d5
        move.b #"0",row
        move.b #"4",row+1
        move.b #"6",col
        move.b #"0",col+1
        jsr cursor
        move.l wdhd,d1
        move.l d4,d2
        move.l d5,d3
        jsr Write(a6)
        lea diskioreq,a0        ;clr error status
        clr.l 32(a0)
        rts

mtroff: move.l 4,a6             ;switch motor off
        lea diskioreq,a1
        move.w #9,28(a1)        ;motor on
        clr.l 36(a1)
        jsr DoIO(a6)
        move.l dosbase,a6
        rts

readsec:move.l 4,a6             ;read block into buffer
        lea diskioreq,a1
        move.w #14,28(a1)       ;test is  disk inserted
        jsr DoIO(a6)
        lea diskioreq,a1
        tst.l 32(a1)
        beq.s dsk

        move.l #nderr,d4        ;output error
        moveq #ndlen,d5
        jsr doerr
        bra here                ;read finished

dsk:    lea diskioreq,a1        ;block read
        move.w #2,28(a1)
        move.l #512,36(a1)
        move.l buffer,40(a1)
        move.l offset,44(a1)
        jsr DoIO(a6)
        tst.l d0                ;test for read error
        beq.s noerr

        move.l #rderr,d4        ;error output
        moveq #rdlen,d5
        jsr doerr
        bra.s here

noerr:  jsr dumpblock           ;output block number,clr error
here:   jsr mtroff
        jsr dumpcheck           ;checksum
        jsr dumptype            ;type
        cmp.b #"a",key          ;ascii input  NTSC added
        jsr dumpasc             ;               NTSC added

        cmp.b #"h",key          ;hex input    NTSC added

        jsr dumphex             ;hex. output
;       jsr dumpasc             ;ascii output orginal pal
        bra main

writesec:                       ;write block to disk
        move.l 4,a6
        lea diskioreq,a1
        move.w #14,28(a1)       ;test if disk inserted
        jsr DoIO(a6)
        lea diskioreq,a1
        tst.l 32(a1)
        beq.s dsk2

        move.l #nderr,d4        ;error output
        moveq #ndlen,d5
        jsr doerr
        bra here2

dsk2:   lea diskioreq,a1
        move.w #15,28(a1)       ;test for write-protect
        jsr DoIO(a6)
        lea diskioreq,a1
        tst.l 32(a1)
        beq.s dsk3

        move.l #pterr,d4        ;error output
        moveq #ptlen,d5
        jsr doerr
        bra here2

dsk3:   move.w #3,28(a1)        ;block write
        move.l #512,36(a1)
        move.l buffer,40(a1)
        move.l offset,44(a1)
        jsr DoIO(a6)
        lea diskioreq,a1
        move.w #4,28(a1)        ;update disk
        move.l #512,36(a1)
        move.l buffer,40(a1)
        move.l offset,44(a1)
        jsr DoIO(a6)
        tst.l d0                ;test for write error
        beq.s noerr2

        move.l #wrerr,d4        ;error output
        moveq #wrlen,d5
        jsr doerr
        bra.s here2

noerr2: jsr dumpblock           ;output everything
here2:  jsr mtroff
        jsr dumpcheck
        jsr dumphex
        jsr dumpasc
        bra main

check:  move.l buffer,a0        ;calculate buffer checksum
        moveq #126,d0
        clr.l d1
adck:   cmp.w #121,d0           ;jump over checksum
        bne.s ck
        add.l #4,a0
ck:     sub.l (a0)+,d1
        dbra d0,adck
        move.l buffer,a0        ;record checksum
        move.l d1,20(a0)
        jsr dumpcheck           ;output
        jsr dumphex
        jsr dumpasc
        bra main

blockedit:                      ;input block number in dec.
        move.b #"0",row
        move.b #"4",row+1
        move.b #"0",col
        move.b #"9",col+1
        jsr cursor
        jsr crsron
        moveq #3,d4             ;4 chars
        lea mytext,a5
in:     jsr getkey
        cmp.b #"0",key
        blt.s in
        cmp.b #"9",key
        bgt.s in
        jsr printkey
        move.b key,(a5)+        ;in text buffer
        dbra d4,in
        jsr crsroff
        clr.w block             ;convert text buffer to hex
        lea mytext,a0
        clr.w d0
        move.b (a0)+,d0
        sub.w #"0",d0
        mulu #1000,d0
        add.w d0,block
        clr.w d0
        move.b (a0)+,d0
        sub.w #"0",d0
        mulu #100,d0
        add.w d0,block
        clr.w d0
        move.b (a0)+,d0
        sub.w #"0",d0
        mulu #10,d0
        add.w d0,block
        clr.w d0
        move.b (a0)+,d0
        sub.w #"0",d0
        add.w d0,block
        cmp.w #1759,block     ;compare with last block
        bgt blockedit         ;new input
        jsr dumpblock
        bra readsec           ;read blockand display

blockedithex:                   ;input block in hex
        move.b #"0",row
        move.b #"4",row+1
        move.b #"1",col
        move.b #"5",col+1
        jsr cursor
        jsr crsron
        lea mytext,a5
        moveq #3,d4             ;4 char
retry:  jsr getkey
        cmp.b #"0",key
        blt retry        cmp.b #"f",key
        bgt retry        cmp.b #"9",key
        ble.s h0
        cmp.b #"a",key
        bge.s h0
        bra.s retry
h0:     jsr printkey
        move.b key,(a5)+        ;write in text buffer
        dbra d4,retry
        jsr crsroff
        move.b mytext,d0        ;convert text to hex in nibbles
        cmp.b #"9",d0
        bgt.s h1
        sub.b #"0"-"a"+10,d0
h1:     sub.b #"a"-10,d0
        lsl.b #4,d0
        move.b d0,block
        move.b mytext+1,d0
        cmp.b #"9",d0
        bgt.s h2
        sub.b #"0"-"a"+10,d0
h2:     sub.b #"a"-10,d0
        or.b d0,block
        move.b mytext+2,d0
        cmp.b #"9",d0
        bgt.s h3
        sub.b #"0"-"a"+10,d0
h3:     sub.b #"a"-10,d0
        lsl.b #4,d0
        move.b d0,block+1
        move.b mytext+3,d0
        cmp.b #"9",d0
        bgt.s h4
        sub.b #"0"-"a"+10,d0
h4:     sub.b #"a"-10,d0
        or.b d0,block+1
        cmp.w #1759,block       ;compare with last block
        bgt blockedithex        jsr dumpblock           ;block output
        bra readsec           ;read block and display

up:     cmp.w #1759,block       ;read next block and display
        beq main        add.w #1,block
        jsr dumpblock
        jmp readsec

down:   tst.w block             ;previous block read and disp.
        beq main        sub.w #1,block
        jsr dumpblock
        jmp readsec

asciiedit: jsr dumpasc          ;ascii input in buffer
        move.b #"0",row         ;PAL =2
        move.b #"6",row+1       ;PAl =3
        move.b #"0",col         ;PAL =0
        move.b #"7",col+1       ;PAL =7
        move.l buffer,buffptr
        jsr crsron
        jsr cursor
getasc: jsr getkey

        cmp.b #$9b,key          ;compare with cursor sequence
        bne nocurs        jsr getkey
        cmp.b #$44,key          ;left
        beq ascleft        cmp.b #$43,key          ;right
        beq ascright        cmp.b #$41,key          ;up
        beq ascup
        cmp.b #$42,key          ;down
        beq ascdown
        bra.s getasc

ascright:                       ;cursor right or start of line
        cmp.b #"7",col
        blt.s csright
        cmp.b #"0",col+1
        blt.s csright
        cmp.b #"3",row
        blt csdown        cmp.b #"0",row+1
        blt csdown        bra getasc            ;cursor in lower left

csright:cmp.b #"9",col+1        ;set cursor
        bne.s m3
        move.b #"0"-1,col+1
        add.b #1,col
m3:     add.b #1,col+1
        add.l #1,buffptr        ;set buffer pointer
        jsr cursor
        bra getasc

csdown: cmp.b #"9",row+1        ;see above
        bne.s m2
        move.b #"0"-1,row+1
        add.b #1,row
m2:     add.b #1,row+1
        move.b #"0",col
        move.b #"7",col+1
        add.l #1,buffptr
        jsr cursor
        bra getasc

ascdown:cmp.b #"3",row          ;cursor down if possible
        blt rowdown        cmp.b #"0",row+1
        blt rowdown        bra getasc

rowdown:cmp.b #"9",row+1
        bne.s m4
        move.b #"0"-1,row+1
        add.b #1,row
m4:     add.b #1,row+1
        add.l #$40,buffptr      ;buffer pointer next line
        jsr cursor
        bra getasc

ascleft:cmp.b #"0",col          ;cursor left or end of line
        bgt.s csleft
        cmp.b #"7",col+1
        bgt.s csleft
        cmp.b #"2",row
        bgt csup        cmp.b #"3",row+1
        bgt csup
        bra getasc              ;cursor is upper left

csleft: cmp.b #"0",col+1        ;set cursor
        bne.s m5
        move.b #"9"+1,col+1
        sub.b #1,col
m5:     sub.b #1,col+1
        sub.l #1,buffptr        ;left buffer pointer
        jsr cursor
        bra getasc

csup:   cmp.b #"0",row+1        ;cursor to end of line
        bne.s m6
        move.b #"9"+1,row+1
        sub.b #1,row
m6:     sub.b #1,row+1
        move.b #"7",col
        move.b #"0",col+1
        sub.l #1,buffptr
        jsr cursor
        bra getasc

ascup:  cmp.b #"2",row          ;cursor up if possible
        bgt rowup
        cmp.b #"3",row+1
        bgt rowup
        bra getasc

rowup:  cmp.b #"0",row+1
        bne.s m8
        move.b #"9"+1,row+1
        sub.b #1,row
m8:     sub.b #1,row+1
        sub.l #$40,buffptr      ;buffer pointer upper line
        jsr cursor
        bra getasc

nocurs: cmp.b #$1b,key          ;escape key= end input
        beq ascend    
        cmp.b #" ",key          ;mask key
        blt getasc
        cmp.b #"z",key
        bgt getasc

        cmp.b #"7",col          ;char left or start of line
        blt.s doright           ;print
        cmp.b #"0",col+1
        blt.s doright
        cmp.b #"3",row
        blt dodown
        cmp.b #"0",row+1
        blt dodown
        jsr printkey
        move.l buffptr,a0       ;store in buffer
        move.b key,(a0)
        bra asciiedit           ;cursor home

doright:cmp.b #"9",col+1        ;char left print
        bne.s m0
        move.b #"0"-1,col+1
        add.b #1,col
m0:     add.b #1,col+1
        jsr printkey
        move.l buffptr,a0
        move.b key,(a0)         ;store in buffer
        add.l #1,buffptr
        jsr cursor
        bra getasc

dodown: cmp.b #"9",row+1        ;print char at start of line
        bne.s m1
        move.b #"0"-1,row+1
        add.b #1,row
m1:     add.b #1,row+1
        move.b #"0",col
        move.b #"7",col+1
        jsr printkey
        move.l buffptr,a0
        move.b key,(a0)         ;store in buffer
        add.l #1,buffptr
        jsr cursor
        bra getasc

ascend: jsr crsroff             ;end the  ascii input
;       jsr dumphex             ;hex output PAL only
        bra main

hexedit:jsr dumphex             ;added NTSC
        move.b #"0",row         ;hex input in buffer
        move.b #"6",row+1       ;similar to ascii input
        move.b #"0",col         ;except:   cursor in 2 steps
        move.b #"7",col+1       ;          to enter in bytes
        move.l buffer,buffptr
        jsr crsron
        jsr cursor
gethex: jsr getkey
        cmp.b #$9b,key
        bne noxcurs
        jsr getkey
        cmp.b #$44,key
        beq hexleft
        cmp.b #$43,key
        beq hexright
        cmp.b #$41,key
        beq hexup
        cmp.b #$42,key
        beq hexdown
        bra gethex

hexright:
        cmp.b #"6",col
        blt.s xcsright
        cmp.b #"9",col+1
        blt.s xcsright
        cmp.b #"2",row
        blt xcsdown
        cmp.b #"1",row+1
        blt xcsdown
        bra gethex

xcsright:
        cmp.b #"9",col+1
        bne.s n3
        move.b #"1"-2,col+1
        add.b #1,col
n3:     add.b #2,col+1
        add.l #1,buffptr
        jsr cursor
        bra gethex

xcsdown:cmp.b #"9",row+1
        bne.s n2
        move.b #"0"-1,row+1
        add.b #1,row
n2:     add.b #1,row+1
        move.b #"0",col
        move.b #"7",col+1
        add.l #1,buffptr
        jsr cursor
        bra gethex

hexdown:cmp.b #"2",row
        blt rowxdown
        cmp.b #"1",row+1
        blt rowxdown
        bra gethex

rowxdown:
        cmp.b #"9",row+1
        bne.s n4
        move.b #"0"-1,row+1
        add.b #1,row
n4:     add.b #1,row+1
        add.l #$20,buffptr
        jsr cursor
        bra gethex

hexleft:cmp.b #"0",col
        bgt.s xcsleft
        cmp.b #"7",col+1
        bgt.s xcsleft
        cmp.b #"0",row
        bgt xcsup
        cmp.b #"6",row+1
        bgt xcsup
        bra gethex

xcsleft:cmp.b #"1",col+1
        bne.s n5
        move.b #"9"+2,col+1
        sub.b #1,col
n5:     sub.b #2,col+1
        sub.l #1,buffptr
        jsr cursor
        bra gethex

xcsup:  cmp.b #"0",row+1
        bne.s n6
        move.b #"9"+1,row+1
        sub.b #1,row
n6:     sub.b #1,row+1
        move.b #"6",col
        move.b #"9",col+1
        sub.l #1,buffptr
        jsr cursor
        bra gethex

hexup:  cmp.b #"0",row
        bgt xrowup
        cmp.b #"6",row+1
        bgt xrowup
        bra gethex

xrowup: cmp.b #"0",row+1
        bne.s n8
        move.b #"9"+1,row+1
        sub.b #1,row
n8:     sub.b #1,row+1
        sub.l #$20,buffptr
        jsr cursor
        bra gethex


noxcurs:cmp.b #$1b,key
        beq hexend    
        cmp.b #"0",key
        blt gethex
        cmp.b #"f",key
        bgt gethex
        cmp.b #"9",key
        ble.s ok0
        cmp.b #"a",key
        bge.s ok0
        bra gethex
ok0:    jsr printkey
ok2:    move.l wdhd,d1
        move.l #key2,d2
        moveq #1,d3
        jsr Read(a6)
        cmp.b #"0",key2
        blt.s ok2
        cmp.b #"f",key2
        bgt.s ok2
        cmp.b #"9",key2
        ble.s ok1
        cmp.b #"a",key2
        blt.s ok2
        
ok1:    move.b key,d0           ;convert key and key2 into byte
        cmp.b #"9",d0
        bgt.s ok3
        sub.b #"0"-"a"+10,d0
ok3:    sub.b #"a"-10,d0
        lsl.b #4,d0
        move.b d0,byte
        move.b key2,d0
        cmp.b #"9",d0
        bgt.s ok4
        sub.b #"0"-"a"+10,d0
ok4:    sub.b #"a"-10,d0
        or.b d0,byte

        cmp.b #"6",col
        blt.s doxright
        cmp.b #"9",col+1
        blt.s doxright
        cmp.b #"2",row
        blt doxdown
        cmp.b #"1",row+1
        blt doxdown
        move.l wdhd,d1
        move.l #key2,d2
        moveq #1,d3
        jsr Write(a6)
        move.l buffptr,a0
        move.b byte,(a0)
        bra hexedit

doxright:
        cmp.b #"9",col+1
        bne.s n0
        move.b #"1"-2,col+1
        add.b #1,col
n0:     add.b #2,col+1
        move.l wdhd,d1
        move.l #key2,d2
        moveq #1,d3
        jsr Write(a6)
        move.l buffptr,a0
        move.b byte,(a0)
        add.l #1,buffptr
        jsr cursor
        bra gethex

doxdown:cmp.b #"9",row+1
        bne.s n1
        move.b #"0"-1,row+1
        add.b #1,row
n1:     add.b #1,row+1
        move.b #"0",col
        move.b #"7",col+1
        move.l wdhd,d1
        move.l #key2,d2
        moveq #1,d3
        jsr Write(a6)
        move.l buffptr,a0
        move.b byte,(a0)
        add.l #1,buffptr
        jsr cursor
        bra gethex

hexend: jsr crsroff             ;end hex input
;       jsr dumpasc             ;ascii output  PAL only
        bra main

;       TEXT, VARIABLES AND TABLE

title:  dc.b "raw:0/0/640/200/"
        dc.b "      DISK-MONITOR VERSION 1.0    "
        dc.b "      INSERT DISK TO EXAMINE IN DF"
drive:  dc.b 0,":   ",0
top:    dc.b $0a

        ;    invers               1.char        normal  rest

        dc.b "            "
        dc.b " ",$9b,"0;31;43",$6d,"Esc",$9b,"0;31;40",$6d,"ape"
        dc.b " ",$9b,"0;31;43",$6d,"#",$9b,"0;31;40",$6d," Block"
        dc.b " ",$9b,"0;31;43",$6d,"$",$9b,"0;31;40",$6d," Block"
        dc.b " ",$9b,"0;31;43",$6d,"+",$9b,"0;31;40",$6d," Up"
        dc.b " ",$9b,"0;31;43",$6d,"-",$9b,"0;31;40",$6d," Down"
        dc.b " ",$9b,"0;31;43",$6d,"R",$9b,"0;31;40",$6d,"ead"
        dc.b " ",$9b,"0;31;43",$6d,"W",$9b,"0;31;40",$6d,"rite"
        dc.b " ",$9b,"0;31;43",$6d,"C",$9b,"0;31;40",$6d,"hecksum"
        dc.b " ",$9b,"0;31;43",$6d,"A",$9b,"0;31;40",$6d,"scii"
        dc.b " ",$9b,"0;31;43",$6d,"H",$9b,"0;31;40",$6d,"ex"
        dc.b $0a,$0a
        dc.b " Block #     $      Buffer #     $      Checksum $"
        dc.b $0a,"---------------------------------------------------"
        dc.b "--------------------------"
topend dc.b   1
toplen=topend-top

        ; BLOCKTYP

boot:   dc.b "BOOTBLOCK "
root:   dc.b "ROOTBLOCK "
flist:  dc.b "FILELIST  "
fhead:  dc.b "FILEHEADER"
dat:    dc.b "DATABLOCK "
udir:   dc.b "USERDIR   "
unkn:   dc.b "----------"

        ; DISK Error messages

nderr:  dc.b $9b,"43",$6d,"NO DISK IN DRIVE !",$9b,"40",$6d
nderrend: dc.b 1ndlen=nderrend-nderr
rderr:  dc.b $9b,"43",$6d,"   READ-ERROR !   ",$9b,"40",$6d
rdend:  dc.b 1rdlen=rdend-rderr
wrerr:  dc.b $9b,"43",$6d,"   WRITE-ERROR !  ",$9b,"40",$6d
wrend:  dc.b 1wrlen=wrend-wrerr
pterr:  dc.b $9b,"43",$6d,"WRITE-PROTECTION !",$9b,"40",$6d
ptend   dc.b 1ptlen=ptend-pterr
clear:  dc.b "    NO ERROR !    "
clrend  dc.b 1clrlen=clrend-clear


 align   ;evencrsrstatus:
        dc.w 0
adr:    dc.w 0
mytext:   dc.b "0000"
key:    dc.b 0
key2:   dc.b 0
byte:   dc.b 0
 align ;even
linebuf:blk.b 64,0      ;buffer for conversions

        ;sequence for cursor positioning

pos:    dc.b $9b
row:    dc.b "00",$3b
col:    dc.b "00",$48

        ;s.o. for address number dec and hex

adrpos: dc.b $9b,"04",$3b,"29",$48
adrpos2:dc.b $9b,"04",$3b,"35",$48

        ;sequence for cursor on/off

con:    dc.b $9b,$20,$70
coff:   dc.b $9b,$30,$20,$70

dosname:dc.b "dos.library",0
trkdisk:dc.b "trackdisk.device",0
device: dc.b 0

 align   ;even
dosbase:dc.l 0
wdhd:   dc.l 0          ;window handle

block:  dc.w 880        ;startblock
offset: dc.l 0          ;offset for read/write =512*block
buffptr:dc.l 0          ;buffer pointer
buffer: dc.l 0          ;buffer start

diskport:
        dc.l 0          ;0
        dc.l 0          ;4
        dc.w $0400      ;8
        dc.l 0          ;10
        dc.b 0          ;14
        dc.b 31         ;15
        dc.l 0          ;16     task adr. here
LH1:    dc.l LH2        ;20
LH2:    dc.l 0          ;24
        dc.l LH1        ;28
        dc.b 0          ;32
        dc.b 0          ;33

diskioreq:
        dc.l 0          ;0
        dc.l 0          ;4
        dc.b 5          ;8
        dc.b 0          ;9
        dc.l 0          ;10
        dc.l diskport   ;14
        dc.w 48         ;18
        dc.l 0          ;20
        dc.l 0          ;24
        dc.w 0          ;28 IO_CMD
        dc.w 0          ;30
        dc.l 0          ;32 IO_ERROR
        dc.l 0          ;36 IO_LENGTH
        dc.l 0          ;40 IO_DATA
        dc.l 0          ;44 IO_OFFSET
        dc.l 0
        dc.l 0
;The following is for NTSC versions only, to clear 8 hex lines
clrhex: dc.b $0a,"                                               "
        dc.b "                           "
        dc.b $0a,"                                               "
        dc.b "                           "
        dc.b $0a,"                                               "
        dc.b "                           "
        dc.b $0a,"                                               "
        dc.b "                           "
        dc.b $0a,"                                               "
        dc.b "                           "
        dc.b $0a,"                                               "
        dc.b "                           "
        dc.b $0a,"                                               "
        dc.b "                           "
        dc.b $0a,"                                               "
        dc.b "                           "
clrhexend: dc.b 1
clrhexlen = clrhexend-clrhex
 end

