; Bitmap test

RAM_Bitmap EQU $FF8000
RAM_VDPCOMMANDS EQU $FF1000
RAM_HCount EQU $FF0120
; this probably makes our life easier... :V
;st             equr a2  ;
cmd            equr a3  ; VDP Commands in RAM
bmp            equr a4  ; Bitmap in RAM
vc             equr a5  ; VDP Control port
vd             equr a6  ; VDP Data Port 


BITMAPTEST:
    ; Setup VDP
   lea      (VDPControl), vc          
   SetVDPReg AutoInc, 2, vc          
   SetVDPReg ScrollSize, 0, vc
   SetVDPReg ModeReg4, 0, vc
   SetVDPReg ModeReg1, $1C, vc
   SetVDPReg ModeReg3, 0, vc
   SetVDPReg ModeReg2, $14, vc

   ; set up a test palette (CGA)
   SetCRAMAddr 0, vc

   move.l #$00000A00, (vd)        
   move.l #$00A00AA2, (vd)        
   move.l #$000B0B2B, (vd)        
   move.l #$024A0CCC, (vd)        
   move.l #$0EEE0E44, (vd)        
   move.l #$04E40EE8, (vd)        
   move.l #$044E0E4E, (vd)        
   move.l #$06EE0666, (vd)        
   
   
   
   lea (RAM_BITMAP), bmp
   lea (BMPTEST), a1
   lea (VDPCONTROL), vc
   lea (RAM_VDPCOMMANDS), cmd
   move.l #RAM_Bitmap, d0       ; Set source address for dma
   moveq #$0020, d1       ; Tile $0 in VRAM
   moveq #23-1, d6       ; Amount of lines
@genDMA:
   rept     4
      DMA2VRAM_SD d0, d1, 128, cmd     ; d0 = source, d1 = destination, 128 = bytes/line, cmd = vdpreg
      add.w #128, d0
      addq.w #8, d1
   endr
   add.w #((64-1)*32),  d1
   dbf d6, @genDMA            ; Keep looping.


   bsr.w Bitmap_InitScrollMap       ; Initialize nametables for SCROLL A & B
   bsr.w Bitmap_InitInts            ; Initialize horizontal interrupt routine
   
   SetVDPReg AutoInc, $20, vc
;   move.w #$8F20, (vc)              ; Use autoincrement of 32
;   move.w #$8A00, (vc)
   EnableInts   
   SetVDPReg ModeReg4, $81, vc
@Loopers:
   cmp.b #192, ($C00008)            ; check HV Counter for line 192
   blt.s @Loopers


   SetVDPReg AutoInc, $20, vc

   lea   (RAM_VDPCOMMANDS), cmd
   move.l #96, d4
@DMALoop
   move.l (cmd)+, (vc)
   move.l (cmd)+, (vc)
   move.l (cmd)+, (vc)
   move.w (cmd)+, (vc)
   dbf d4, @DMALOOP

;   SetVDPReg ModeReg2, $54, vc
;   SetVDPReg ModeReg4, $00, vc
   WaitFrame
   clr.b (RAM_HCount)
   bra @Loopers


BitmapHINT:
   move.w #$8F02, (vc)
   SetVSRAMAddr 0, vc
   tst.b (RAM_HCount)
   beq.s @its0
   clr.b (RAM_HCount)
   move.l #$0, (vd)                         
   move.w #$8F20, (vc)
   rte
@its0
   move.l #-1, (vd)
   addq.b #4, (RAM_HCount)
   move.w #$8F20, (vc)
   rte
BITMAPHINT_END:



Bitmap_InitInts:
   move.w   #$8A00, (vc)
   move  #$2700, sr
   lea (BitmapHINT), a0
   lea (RAM_HINT), a1
   move.l #((BitmapHINT_End-BitmapHINT)/2)-1,d0   
@CopyHINT:
   move.w (a0)+, (a1)+
   dbf d0, @CopyHINT   
   move.w  #$8154, (vc)             ; Enable display, disable interrupts
   rts


; Initialise the tilemap
; $C000 for "A" buffer, $E800 for "B" buffer
Bitmap_InitScrollMap:
   setVRAMAddr $C000, VDPControl
   lea VDPData, a6
   moveq    #$1, d0
   moveq #24-1, d2
@WriteAMap:
   moveq #32-1, d1
@WriteAMap_:
   move.w   d0, (a6)
   addq.w   #2, d0
   dbf d1, @WriteAMap_
   dbf d2, @WriteAMap

   setVRAMAddr $E000, VDPControl   
   moveq    #2, d0
   moveq #24-1, d2
@WriteBMap:
   moveq #32-1, d1
@WriteBMap_:
   move.w   d0, (a6)
   addq.w   #2, d0
   dbf d1, @WriteBMap_
   dbf d2, @WriteBMap
   
   move.w #($8200|($C000/$400)), (vc)
   move.w #($8400|($E000/$2000)), (vc)
   setvramaddr $f800, vc
   move.l #$00000004, (vd)
   rts


; d0 = x coordinate
; d1 = y coordinate
; d2 = pixel data (palette offset)
WritePixel:
   move.l   d3, -(sp)
   movem.l  d0-d1, -(sp)
   
   moveq    #0, d3
   move.b   d2, d3
   lsl.b    #4, d3         ; Shift to left 4 bits
   or.b     d3, d2         ; Double pixel width meepsphere
   
   lea      RAM_Bitmap, a0
   moveq    #0, d3
   lsl.w    #7, d1         ; Multiply d1 by $80 (num of bytes per row)
   add.w    d0, d1         ; Add to it the X offset
   
   move.b   d2, (a0, d1.w) ; Write to RAM buffer
   
   movem.l   (sp)+, d0-d1
   move.l   (sp)+, d3
   rts

LoadBitmap:
   ; A1 = Source
   ; Breaks a1 :P
   move.w d7, -(sp)
   move.w #(128*96)/4, d7  ; Amount of Pixels to load
@LoadBMPLoop:
   move.l (a1)+, (bmp)+
   dbf d7, @LoadBMPLoop
   move.w (sp)+, d7

BMPTEST:
   incbin ./a.bin
   even


