
*Chaos strikes back spec. loader

*SET   RRDISK
  move.l #511,d1
  move.l d1,d2
  sub.w #3,d2
  move.w d2,dcnum *Set sector count 
  addq.l #1,d1    *  in BPB.
  mulu #1024,d1
  move.l d1,sizm *Keep length
  pea postrd(pc)
  move.w #38,-(sp)
  trap #14
  addq.l #6,sp
todesk  clr.w -(sp)
  trap #1

postrd 
  bset #2,$4c5.w  *Drive C in registry.
  move.w #2,$446.w  *Disk C for
*       * bootdevice.
  move.l #$80000,a6
  move.l a6,a2
  move.l #$a1b2c3d4,(a6)+ *Set marker
  move.l $42e.w,(a6)+  *Keep old phystop

  move.l a2,$42e.w  *Phystop 

*Set code for init after reset
  lea $600.w,a1 
  lea unires(pc),a0
  move.w #exe-unires-1,d0
uc move.b (a0)+,(a1)+
  dbf d0,uc

*Enter address in catalog
  lea $640.w,a0
ul cmp.l #0,(a0)
  beq.s tupisi
  addq.l #4,a0
  cmp.l #$6fc,a0
  beq plz
  bra.s ul
tupisi move.l a6,(a0)+
  clr.l (a0)

*Checksum
  move.l #255,d0
  clr.l d2
  lea $600.w,a0 
chsl add.w (a0)+,d2 *Calc checksum.
  dbf d0,chsl
  sub.w #$5678,d2
  sub.w d2,$63e.w   *Set checksum. . 
  
  lea exe(pc),a0
  move.w #exit-exe,d2
  lsr.w #2,d2
excopy  move.l (a0)+,(a6)+
  dbf d2,excopy *Copy ramdisk driver.

*Load ramdisk content

  clr.w -(sp)
  pea name
  move.w #61,-(sp)
  trap #1
  addq.l #8,sp
  tst.w d0
  bmi.s err

  pea $80400
  pea 456704
  move.w d0,-(sp)
  move.w #63,-(sp)
  trap #1
  lea 12(sp),sp
  tst.l d0
  bmi.s err   

*Patch against protect  copy
  lea $82260,a0
  lea $ff000,a1
  move.w #$4ef9,(a0)+
  move.l a1,(a0)
  lea patch(pc),a0
  move.w #pat-patch,d0
pcl move.b (a0)+,(a1)+
  dbf d0,pcl

tores move.l 4.w,a1 
  jmp (a1)   *Reset!
plz clr.l (a0)
  bra.s tores
err rts    
name dc.b "CHAOSSTB.RRD",0
  even


unires dc.l $12123456
  dc.l $00000600
  lea $640.w,a0
cl cmp.l #0,(a0)
  beq.s urend
  move.l (a0)+,a1
  move.l a0,-(sp)
  jsr (a1)
  move.l (sp)+,a0
  bra.s cl
urend rts  

exe 
                   

* shift test for no autostart
  pea $bffff
  trap #13
  addq.l #4,sp
  and.b #3,d0 *both shift keys
  move.l $42e.w,a0
  addq.l #8,a0  *This is label exe.
  move.l a0,a1  *Keep address.
  lea oldb-exe(a0),a0
  move.l $472.w,(a0)+
  move.l $476.w,(a0)+
  move.l $47e.w,(a0)+  *Keep old 
  move.l $b8.w,(a0)
  lea newb-exe(a1),a1  *vectors.
  move.l a1,$472.w
  lea newrw-newb(a1),a1
  move.l a1,$476.w
  lea newch-newrw(a1),a1
  move.l a1,$47e.w   *New vectors set!
  lea newxb-newch(a1),a1
  move.l a1,$b8.w  *New XBIOS
  move.w #2,$4a6.w  *Simulate that  floppy B is attached
  tst.b d0
  bne.s noauto 
  jmp $80400
noauto rts



*Ramdisk driver. 
newb move.w 4(sp),d0
  cmp.w #1,d0  *Is disk B?
  bne.s itcb
  move.w #0,4(sp)  *Forward to A!
  move.l oldb(pc),a0
  jmp (a0)
itcb lea rbpb(pc),a0
  move.l a0,d0
  rts  
  
newrw move.w 14(sp),d0
  cmp.w #1,d0
  bne.s itcr
  clr.w 14(sp)
  move.l oldrw(pc),a0
  jmp (a0)
itcr  sf d3  *flag
itcr1  clr.l d1
  move.w 10(sp),d1
  lsl.l #5,d1  *Mult by 32.
  move.l 6(sp),a0
  move.l a0,d2

  lea exe+1016(pc),a1  *Ramdisk start.
  clr.l d0
  move.w 12(sp),d0
  lsl.l #7,d0
  lsl.l #2,d0
  add.l d0,a1
  btst #0,5(sp)
  bne.s writ
  exg a1,a0
writ btst #0,d2  *Even adr.?
  bne.s nepar
  subq.l #1,d1
movl move.l (a0)+,(a1)+
  move.l (a0)+,(a1)+
  move.l (a0)+,(a1)+
  move.l (a0)+,(a1)+
  dbf d1,movl
noch clr.l d0
  tst.b d3
  bne.s xex
  rts
xex 
  lea 20(sp),sp
  movem.l (sp)+,d1-d7/a1 
  rte  


nepar lsl.l #2,d1     
  subq.l #1,d1
movb move.b (a0)+,(a1)+
  move.b (a0)+,(a1)+
  move.b (a0)+,(a1)+
  move.b (a0)+,(a1)+
  dbf d1,movb
  bra.s noch
  
newch move.w 4(sp),d0
  cmp.w #1,d0
  bne.s nocn
  clr.w 4(sp)
  move.l oldch(pc),a0
  jmp (a0)
nocn clr.l d0
  rts 

oldb ds.l 1
oldrw ds.l 1
oldch ds.l 1
oldx ds.l 1
  
rbpb dc.w 512  *Bytes per sector.
  dc.w 2 *Sectors per cluster.
  dc.w 1024 *Bytes per cluster.
  dc.w 1  *Root dir lenght in sectors
  dc.w 2  *FAT len in sectors
  dc.w 3  *Start of second FAT.
  dc.w 6 *First free sector #.
dcnum  dc.w 0  *Total cluster count here. 
  dc.w 0  *Disk FAT
  dc.w 0  


*New XBIOS

newxb  move.l sp,a0
  btst #5,(sp) *test is called from user
  bne.s notuser
  move.l usp,a0
  subq.l #6,a0  *correct stack
notuser move.w 6(a0),d0
  cmp.w #8,d0
  beq.s flrd
  cmp.w #9,d0
  bne.s torom
  clr.l d0
  rte
torom  move.l oldx(pc),a0
  jmp (a0)
flrd cmp.w #1,16(a0)
  bne.s flas
  clr.w 16(a0)
  bra.s torom 
flas   
  movem.l a1/d1-d7,-(sp)
  lea -20(sp),sp  *If called from super mode
  st d3
  addq.l #6,a0
  move.l 2(a0),6(sp)
  move.w 18(a0),10(sp)
  move.w 12(a0),12(sp)
  cmp.w #$f7,12(sp)
  bne.s notprs
  move.w #8,12(sp)  *!!
notprs cmp.w #7,12(sp)
  bne.s not
  eor.w #$8080,$81066
  eor.w #$8080,$81084 
not  subq.w #1,12(sp)
  clr.w 4(sp)  *flag for read
  bra itcr1 
  
sizm ds.l 1
exit
  clr.w -(sp)
  trap #1


patch  

  MOVE.L A2,-(SP)
  ADD.L #$58A,A2  *JMPtable (code checksummed)
  MOVE.L #$FF000+SKIP-patch,(A2)
  ADD.L #$2E9C8-$1C99E,A2  *1 checksum
  MOVE.W #$6010,(A2)
  ADD.L #$36D64-$2E9C8,A2  *2 checksum
  MOVE.W #$600E,(A2)
  ADD.L #$1F42E-$36D64,A2
  MOVE.W #$6006,(A2)
  ADD.L #$22C5E-$1F42E,A2
  MOVE.W #$6006,(A2)
  ADD.L #$2DC92-$22C5E,A2 *this 5 patch is for "PACE/FB" disktest
  MOVE.W #$6006,(A2)
  ADD.L #$361F4-$2DC92,A2
  MOVE.W #$6006,(A2)
  ADD.L #$36466-$361F4,A2
  MOVE.W #$6006,(A2)
  ADD.L #$358D4-$36466,A2
  MOVE.W #$4E75,(A2)
  ADD.L #$2FC6E-$358D4,A2
  MOVE.W #$4E75,(A2)
  ADD.L #$2A106-$2FC6E,A2
  MOVE.W #$4E75,(A2)
  ADD.L #$2C3FE-$2A106,A2
  MOVE.W #$6008,(A2)
  ADD.L #$33F8E-$2C3FE,A2  *Against die without reason
  MOVE.W #$6002,(A2)
  ADD.L #$39EBC-$33F8E,A2
  MOVE.W #$4E75,(A2) *Against "put CSB disk in drive A"

*Following patches are for stronger heroes!
  ADD.L #$326AA-$39EBC,A2
  MOVE.W #$4E71,D0
  MOVE.W D0,(A2)
  LEA $F4-$AA(A2),A2
  MOVE.W D0,(A2)
  LEA $12A-$F4(A2),A2
  MOVE.W D0,(A2)
*Back to program
  MOVE.L (SP)+,A2
  JSR (A2)

SKIP DC.W $4E56,$FEAE,$48E7,$0F30
  MOVE.L A5,A0
  ADD.L #$2820C-$A202,A0  *Skip disktest by save.
  JMP (A0)
  DC.W $4E5E
  RTS
pat

