; Bitmap test

RAM_Bitmap EQU $FF8000
RAM_VDPCOMMANDS EQU $FF1000
RAM_HCount EQU $FF0120


BITMAPTEST:
    ; Setup VDP
   lea      (VDPControl), a5
   move.w   #$8F02, (a5) ; auto inc of 2
   move.w   #$9000, (a5) ; 32x32 planes
   move.w   #$8C00, (a5) ; 32H mode
   move.w   #$801C, (a5) ; enable HV counter
   move.w   #$8B00, (a5)         ; Full screen scrolling
   move.w   #$8114, (a5)   ; Disable display, disable ints.

; sliceymajig
; I'm rewriting everything. Fuck it. 
; Yay? I guess the code I wrote was pretty fucking shitty so eh.


   ; set up a test palette (CGA)
   SetCRAMAddr 0, (a5)
   move.l #$00000A00, -4(a5)
   move.l #$00A00AA2, -4(a5)
   move.l #$000B0B2B, -4(a5)
   move.l #$024A0CCC, -4(a5)
   move.l #$0EEE0E44, -4(a5)
   move.l #$04E40EE8, -4(a5)
   move.l #$044E0E4E, -4(a5)
   move.l #$06EE0666, -4(a5)
   
   ; VRAM organisation
   ; Bytes
   ; 24576     tiles 1 ("A")
   ; 24576     tiles 2 ("B")
   ; = 0000 - C000
   

   moveq #128-1, d0 ; x coordinate
   moveq #96-1, d1 ; y coordinate
   moveq #1, d2 ; colour value
   lea (RAM_BITMAP), a4
   lea (BMPTEST), a3
@fuck:
   moveq #128-1, d0
@you:
   move.b   d1, d2
   and.b    #$F, d2
   move.b (a3)+, (a4)+
   dbf d0, @you
   dbf d1, @fuck


   lea   (RAM_VDPCOMMANDS), a6
   move.l   #RAM_Bitmap, d0      ; Set sorce.
   moveq   #$0000, d1           ; Tile $0 in VRAM
   moveq #23-1, d6

@genDMAA:
   rept 4
   DMA2VRAM_SD d0, d1, 128, a6
   add.w #128, d0
   addq.w #8, d1
   endr
   
;   add.w #128, d0
;   addq.w #8, d1
   add.w #((64-1)*32), d1
   dbf      d6, @genDMAA            ; Keep looping.

kajir:

   bsr.w Bitmap_InitScrollMap
   bsr.w Bitmap_InitInts
   
   lea (VDPCONTROL), a5
   move.w   #$8F20, (a5)         ; Use autoincrement of 16!!!
   
   move.w #$8A00, (a5)
   lea (ScrollTable), a4
   enableints
   
   move.w   #$8F20, (a5)         ; Use autoincrement of 16!!!
   move.w #$8C81, (VDPCONTROL)
@Loopers:
   cmp.b #192, ($C00008)
   blt.s @Loopers
   
;   disableints
   move.w #$8c81, (A5)
   lea   (RAM_VDPCOMMANDS), a6
   move.l #96, d4
@DMALoop
   move.l (a6)+, (a5)
   move.l (a6)+, (a5)
   move.l (a6)+, (a5)
   move.w (a6)+, (a5)
   dbf d4, @DMALOOP
;   move.w #$8154, (VDPCONTROL)
   move.w #$8c00, (A5)
;@s:
   WaitFrame
   clr.l ($FF7000)
;   enableints
;   jmp @s
   bra @Loopers


BitmapHINT:
   move.w #$8F02, (a5)
   SetVSRAMAddr 0, (a5)
   tst.b ($FF7000)
   beq.s @its0
   clr.b ($FF7000)
   move.l #$0, (VDPDATA);FFFFFFFF, (VDPDATA)
   move.w #$8F20, (a5)
   rte
@its0
   move.l #-1, (VDPDATA)
   addq.b #4, ($FF7000)
   move.w #$8F20, (a5)
   rte
BITMAPHINT_END:

ScrollTAble:
   dc.l $00000000, $FFFFFFFF



Bitmap_InitInts:
   move.w   #$8A00, (VDPControl) ; HInt every 4 sliceamajiggers
   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.l #BitmapVINT, (RAM_VINTLOC+2)
   move.w  #$8154, (VDPControl)             ; 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    #$0, 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    #1, 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)), (VDPControl)
   move.w #($8400|($E000/$2000)), (VDPControl)
;  setvsramaddr 0, (VDPCONTROL)
;  move.l #$FFFF0000, (VDPDATA)
   setvramaddr $f800, (VDPCONTROL)
   move.l #$00000004, (VDPDATA)
   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

BMPTEST:
   incbin ./a.bin
   even


; a0 = Tile buffer location in RAM (DMA'd to VDP)
; a1 = Bitmap location
ConvertFB2Tile:
   lea   FB2TileLUT(pc), a2   ; PC relative saves like 4 cycles =V
   
   move.w   #(128*64)-1, d0 ; number of iterations of the loop. (screen size)

@loopinator:
   move.w   (a2)+, d0   ; Get the offset for the correct pixel to go into the tile
   move.b   (a1, d0.w), (a0)+ ; Get that byte from bitmap, write to tile buffer
   dbf      d0, @loopinator   ; Just rinse and repeat! =V
   
   rts
   
FB2TileLUT:
   ; put our LUT schits here.
