; EQU:
; Data addresses used by the opcodes that point to uninitialized memory areas.
; Last element of stack:
DATA1:       equ  00FEh
SUB2:        equ  D600h	; 54784. Subroutine. Called by: LBL1[C124h].


; ===========================================================================
; SVI-328 I/O Port Definitions (Boot Sector)
; ===========================================================================

; Floppy Disk Controller (ports 30h-38h)
FDC_CMD_STATUS  equ  30h    ; W: Command Register / R: Status Register
FDC_TRACK       equ  31h    ; R/W: Track Register
FDC_SECTOR      equ  32h    ; R/W: Sector Register
FDC_DATA        equ  33h    ; R/W: Data Register
FDC_INTRQ_DRQ   equ  34h    ; R: INTRQ/DRQ status
FDC_DISK_SELECT equ  34h    ; W: Disk Select (b0=Disk1, b1=Disk2)
FDC_DENSITY     equ  38h    ; W: Density Select (b0: 0=DD, 1=SD)

; PSG (ports 88h/8Ch/90h)
PSG_ADDR        equ  88h    ; W: Register Address Select
PSG_WRITE       equ  8Ch    ; W: Register Data Write
PSG_READ        equ  90h    ; R: Register Data Read

; ===========================================================================
; SVI-328 I/O Port Definitions (Boot Sector)
; ===========================================================================

; Floppy Disk Controller (ports 30h-38h)
FDC_CMD_STATUS  equ  30h    ; W: Command Register / R: Status Register
FDC_TRACK       equ  31h    ; R/W: Track Register
FDC_SECTOR      equ  32h    ; R/W: Sector Register
FDC_DATA        equ  33h    ; R/W: Data Register
FDC_INTRQ_DRQ   equ  34h    ; R: INTRQ/DRQ status
FDC_DISK_SELECT equ  34h    ; W: Disk Select (b0=Disk1, b1=Disk2)
FDC_DENSITY     equ  38h    ; W: Density Select (b0: 0=DD, 1=SD)

; PSG (ports 88h/8Ch/90h)
PSG_ADDR        equ  88h    ; W: Register Address Select
PSG_WRITE       equ  8Ch    ; W: Register Data Write
PSG_READ        equ  90h    ; R: Register Data Read

             org C100h; C100h


; Label not accessed.
C100 LBL1:
C100              ld   a,0Fh  	; 15
C102              out  (PSG_ADDR),a 	; 136
C104              ld   a,DDh  	; 221,  -35
C106              out  (PSG_WRITE),a 	; 140
C108              ld   sp,DATA1+2 	; 0100h, top of stack
C10B              ld   b,11h  	; 17
C10D              call SUB1   	; C127h
C110              ld   a,00h  	; 0
C112              out  (FDC_DENSITY),a 	; 56
C114              ld   hl,0100h 	; 256
C117              ld   (SELF_MOD2+1),hl 	; C14Fh, WARNING: Instruction accesses code!
C11A              ld   a,12h  	; 18
C11C              ld   (SELF_MOD3+1),a 	; C15Bh, WARNING: Instruction accesses code!
C11F              ld   b,0Ch  	; 12
C121              call SUB1   	; C127h
C124              jp   SUB2   	; D600h


; Subroutine: Size=86, CC=6.
; Called by: LBL1[C10Dh], LBL1[C121h].
; Calls: -
C127 SUB1:
C127              push bc     
C128 .sub1_loop1:
C128              ld   a,02h  	; 2
C12A              out  (FDC_SECTOR),a 	; 50
C12C              ld   a,80h  	; 128, -128
C12E              out  (FDC_CMD_STATUS),a 	; 48
C130              ld   c,33h  	; 51, '3'
C132              ex   (sp),hl 
C133              ex   (sp),hl 
C134              ex   (sp),hl 
C135              ex   (sp),hl 


; Data accessed by:
; C14Bh(in SUB1), C152h(in SUB1)
C136 SELF_MOD1:
C136              ld   hl,D600h 	; 54784, -10752
C139 .sub1_loop2:
C139              in   a,(FDC_INTRQ_DRQ) 	; 52
C13B              add  a,a    
C13C              jr   c,.sub1_l1 	; C145h
C13E              jp   p,.sub1_loop2 	; C139h
C141              ini         
C143              jr   .sub1_loop2 	; C139h
C145 .sub1_l1:
C145              in   a,(FDC_CMD_STATUS) 	; 48
C147              and  1Ch    	; 28
C149              jr   nz,.sub1_loop1 	; C128h
C14B              ld   hl,(SELF_MOD1+1) 	; C137h, WARNING: Instruction accesses code!


; Data accessed by:
; C117h(in LBL1)
C14E SELF_MOD2:
C14E              ld   de,0080h 	; 128
C151              add  hl,de  
C152              ld   (SELF_MOD1+1),hl 	; C137h, WARNING: Instruction accesses code!
C155              ld   hl,C129h 	; 49449, -16087
C158              inc  (hl)   
C159              ld   a,(hl) 


; Data accessed by:
; C11Ch(in LBL1)
C15A SELF_MOD3:
C15A              cp   13h    	; 19
C15C              jr   c,.sub1_l2 	; C179h
C15E              ld   a,52h  	; 82, 'R'
C160              out  (FDC_CMD_STATUS),a 	; 48
C162              ld   (hl),01h 	; 1
C164              ex   (sp),hl 
C165              ex   (sp),hl 
C166              ex   (sp),hl 
C167              ex   (sp),hl 
C168              ex   (sp),hl 
C169              ex   (sp),hl 
C16A              ex   (sp),hl 
C16B              ex   (sp),hl 
C16C              ex   (sp),hl 
C16D              ex   (sp),hl 
C16E              ex   (sp),hl 
C16F              ex   (sp),hl 
C170              ex   (sp),hl 
C171              ex   (sp),hl 
C172              ex   (sp),hl 
C173              ex   (sp),hl 
C174 .sub1_loop3:
C174              in   a,(FDC_CMD_STATUS) 	; 48
C176              rra         
C177              jr   c,.sub1_loop3 	; C174h
C179 .sub1_l2:
C179              pop  bc     
C17A              djnz SUB1   	; C127h
C17C              ret         


C17D              defb 00h    	; 0
C17E              defb 00h    	; 0
C17F              defb 00h    	; 0
; ...
; ...
; ...