; Reset Shifter
; when the system resets an area of memory will be moved
; low to high where it is safe from OS overwrite...
; Coded by Trojan of The Medway Boys

; library defs...

; exec

OldOpenLibrary=-408
CloseLibrary=-414

; dos

Open=-30
Close=-36
Read=-42
Write=-48

; open dos library...

move.l $4.w,a6			; exec base
lea DosName,a1		; name
jsr OldOpenLibrary(a6)
move.l d0,DosBase		; save base
beq error			; not opened

; open console window...

move.l d0,a6

move.l #ConName,d1		; console name
move.l #1005,d2			; open mode (old)
jsr Open(a6)
move.l d0,ConHand		; save console handle
beq error			; not opened

; print title...

bsr bold
move.l ConHand,d1
move.l #titletxt,d2
move.l #ttsize,d3
jsr Write(a6)
bsr norm

; ask for address to wedge routine to...

askaddr:
move.l ConHand,d1
move.l #wedgetxt,d2		; title text
move.l #wtsize,d3		; no. of chars
jsr Write(a6)

move.l ConHand,d1
move.l #buffer,d2		; where to put text
move.l #80,d3			; max 80 chars
jsr Read(a6)

bsr gethex			; get address
move.l d0,wedgeto		; save address
tst.l d1			; OK ?
bmi askaddr

; ask for address to shift from...

askfrom:
move.l ConHand,d1
move.l #fromtxt,d2		; title text
move.l #ftsize,d3		; no. of chars
jsr Write(a6)

move.l ConHand,d1
move.l #buffer,d2		; where to put text
move.l #80,d3			; max 80 chars
jsr Read(a6)

bsr gethex			; get address
move.l d0,from			; save address
tst.l d1			; OK ?
bmi askfrom

; ask for no. of bytes to save...

asklen:
move.l ConHand,d1
move.l #lentxt,d2		; title text
move.l #ltsize,d3		; no. of chars
jsr Write(a6)

move.l ConHand,d1
move.l #buffer,d2		; where to put text
move.l #80,d3			; max 80 chars
jsr Read(a6)

bsr gethex			; get address
move.l d0,length		; save address
tst.l d1			; OK ?
bmi asklen

; ask address to shift to

askto:
move.l ConHand,d1
move.l #totxt,d2
move.l #totsize,d3
jsr Write(a6)

move.l ConHand,d1
move.l #buffer,d2
move.l #80,d3
jsr Read(a6)

bsr gethex
move.l d0,to
tst.l d1
bmi askto

; show confirmation text...

move.l ConHand,d1
move.l #conf1txt,d2		; pointer to text
move.l #ct1size,d3		; no. of chars
jsr Write(a6)

bsr bold
move.l wedgeto,d0		; hex to print
bsr printhex			; print on screen
bsr norm
bsr newline

move.l ConHand,d1
move.l #conf2txt,d2		; pointer to text
move.l #ct2size,d3		; no. of chars
jsr Write(a6)

bsr bold
move.l from,d0 			; hex to print
bsr printhex			; print on screen
bsr norm
bsr newline

move.l ConHand,d1
move.l #conf3txt,d2		; pointer to text
move.l #ct3size,d3		; no. of chars
jsr Write(a6)

bsr bold
move.l length,d0		; hex to print
bsr printhex			; print on screen
bsr norm
bsr newline			; print newline

move.l ConHand,d1
move.l #conf4txt,d2
move.l #ct4size,d3
jsr Write(a6)

bsr bold
move.l to,d0
bsr printhex
bsr norm
bsr newline


getYN:
move.l ConHand,d1
move.l #yesnotxt,d2
move.l #ynsize,d3
jsr Write(a6)
bsr newline

; wait for key to be pressed...

move.l ConHand,d1
move.l #buffer,d2
move.l #80,d3
jsr Read(a6)

move.b buffer,d0
cmp.b #"Y",d0
beq install
cmp.b #"y",d0
beq install
cmp.b #"N",d0
beq askaddr
cmp.b #"n",d0
beq askaddr
cmp.b #"Q",d0
beq quit
cmp.b #"q",d0
beq quit
bra getYN	

install:
move.l from,copyfrom+2		; complete instructions
move.l to,copyto+2		; in shifter code...
move.l length,copylen+2

lea shifter,a0
move.l wedgeto,a1
move.w #shiftsize/2,d0		; wordsize of routine

wedge:
move.w (a0)+,(a1)+
dbf d0,wedge

move.l $4.w,a6
move.l wedgeto,a0
move.l a0,42(a6)
lea 34(a6),a2
clr.w d1
move.w #$17,d2

resloop:
add.w (a2)+,d1
dbf d2,resloop
not.w d1
move.w d1,(a2)

; wait for left mouse button

move.l DosBase,a6
move.l ConHand,d1
move.l #quittxt,d2
move.l #qtsize,d3
jsr write(a6)

testmouse:
btst #6,$bfe001
bne testmouse

; close the console window...

quit:
move.l ConHand,d1
jsr Close(a6)

move.l $4.w,a6
move.l DosBase,a1
jsr CloseLibrary(a6)

clr.l d0			; no return code
rts

error:
move.l #-1,d0			; return code
rts

; get 8 digit hex number
; returns no. in D0, 0 in D1 if no error else -1...

gethex:
lea buffer,a0		; start of buffer
move.w #7,d2			; 8 chars to get
clr.l d0

getloop:
clr.l d1
move.b (a0)+,d1		; get char
cmp.b #$a,d1			; CR ?
beq getexit
lsl.l #4,d0			; shift total up 1 nibble
cmp.b #"0",d1
bmi geterror		; out of range
cmp.b #"9"+1,d1
bmi digit			; char is digit
cmp.b #"A",d1
bmi geterror		; out of range
cmp.b #"f"+1,d1
bpl geterror		; out of range
cmp.b #"F"+1,d1
bmi upper			; char is uppercase letter
cmp.b #"a",d1
bpl lower			; char is lowercase letter

geterror:
move.l #-1,d1			; return code
rts

digit:
subi.b #"0",d1			; convert to hex
bra addit

upper:
subi.b #"A"-$a,d1		; convert to hex
bra addit

lower:
subi.b #"a"-$a,d1		; convert to hex

addit:
add.b d1,d0			; put in total
dbf d2,getloop

getexit:
clr.l d1			; no return code
rts

; print hex number in d0...

printhex:
lea store,a0		; temp store for ASCII
move.w #7,d1			; print 8 chars

prnloop:
clr.l d2			; clear total
rol.l #4,d0			; get digit
move.b d0,d2
andi.b #$f,d2			; mask out high nibble
cmp.b #$a,d2			; digit or letter ?
blt p_digit			; digit!
addi.b #"A"-$a,d2		; convert to ASCII
bra storeit			; and store

p_digit:
addi.b #"0",d2			; convert to ASCII

storeit:
move.b d2,(a0)+		; store
dbf d1,prnloop

; now print the store....

move.l DosBase,a6
move.l ConHand,d1
move.l #store,d2		; pointer to ASCII
move.l #8,d3			; 8 chars
jsr Write(a6)

rts

; print a newline...

newline:
move.l DosBase,a6
move.l ConHand,d1
move.l #nltxt,d2
move.l #nlsize,d3
jsr Write(a6)
rts

; set bold...

bold:
move.l DosBase,a6
move.l ConHand,d1
move.l #boldtxt,d2
move.l #btsize,d3
jsr Write(a6)
rts

; set normal...

norm:
move.l DosBase,a6
move.l ConHand,d1
move.l #normtxt,d2
move.l #ntsize,d3
jsr Write(a6)
rts


; ********************** THIS IS THE WEDGED ROUTINE *****************

shifter:
movem.l d0-d2/a0/a6,-(a7)
lea real(pc),a0			; wedge in the real shift routine
move.l $4.w,a6
move.l a0,42(a6)
lea 34(a6),a2
clr.w d1
move.w #$17,d2

chksum:
add.w (a2)+,d1
dbf d0,chksum
not.w d1
move.w d1,(a2)
 
move.w #10,d0

outer:
move.w #$ffff,d1

inner:
dbf d1,inner
bchg #1,$bfe001
dbf d0,outer
bclr #1,$bfe001
movem.l (a7)+,d0-d2/a0/a6
jmp (a5)

real:
movem.l d0/a0-a1,-(a7)

copyfrom:
lea $000000,a0

copyto:
lea $000000,a1

copylen:
move.l #$000000,d0

copyit:
move.b (a0)+,(a1)+
addi.w #1,$dff180
subq.l #1,d0
bne.s copyit

movem.l (a7)+,d0/a0-a1
jmp (a5)

shiftsize=*-shifter

DosName: dc.b "dos.library",0

ConName: dc.b "CON:20/20/420/120/MWB's Reset Shifter Rev. 1",0

titletxt: dc.b "Coded by Trojan of The Medway Boys with SEKA v2.1"
ttsize=*-titletxt

wedgetxt: dc.b "Address to wedge routine: $"
wtsize=*-wedgetxt

fromtxt: dc.b "Address to shift FROM: $"
ftsize=*-fromtxt

lentxt:	dc.b "Bytes to shift: $"
ltsize=*-lentxt

totxt: dc.b "Address to shift TO: $"
totsize=*-totxt

conf1txt: dc.b "Please Confirm....",$a,$d
dc.b "Wedge address.... $"
ct1size=*-conf1txt

conf2txt: dc.b "Shift FROM....... $"
ct2size=*-conf2txt

conf3txt: dc.b "Bytes to shift... $"
ct3size=*-conf3txt

conf4txt: dc.b "Shift TO......... $"
ct4size=*-conf4txt

yesnotxt: dc.b "Enter Y to install, N to correct or Q to quit"
ynsize=*-yesnotxt

nltxt: dc.b $a,$d
nlsize=*-nltxt

boldtxt: dc.b $9b,"1;31;40m"
btsize=*-boldtxt

normtxt: dc.b $9b,"0;31;40m"
ntsize=*-normtxt

quittxt: dc.b "DONE... press left mouse button"
qtsize=*-quittxt

even

DosBase: dc.l 0
ConHand: dc.l 0
wedgeto: dc.l 0
from: dc.l 0
length:	dc.l 0
to: dc.l 0
store: blk.b 8
buffer:	blk.b 80

