02. Line optimized (line_opt) - RetroAsmDev/C64AsmGameJourney GitHub Wiki

This version fills the line with red color in optimized way. The code consumes less cycles, so colors are set to register before the electron beam reaches the left edge of the visible part of the screen and in result the line is drawn nicely from the left to right as we wanted.

The program is optimized in a way:

  1. It pre-loads the color into X register and the line number into A register
  2. It compares the A register directly against the $D012 address

Code:

!to "line_opt2.prg"

RASTER_D012      = $D012
EXTCOL_D020      = $D020 

;this creates a basic start
*=$0801

      ; 10 SYS 2064 
      ; Tokenized command 10 SYS 2064 (=$810)
      ; SYS that tells the processor to execute the machine language subroutine at a specific address
      !byte $0C,$08,$0A,$00,$9E,$20,$32,$30,$36,$34,$00,$00,$00,$00,$00

      sei     ; Disble CIA interrupt          
      
      ; Here the game logic has to be done during the VBLANK interval 
GameLoop  
      
      jsr WaitFrame ; If the game logic is finished, go wait for next VBLANK
      jmp GameLoop          

WaitFrame 
      lda #251 ; Pre-load line index 251
      ldx #2   ; Pre-load color
 Line251
      cmp RASTER_D012   
      bne Line251
      
      stx EXTCOL_D020
      
      ldx #14  ; Pre-load color (light blu = default colo
 Line251_2
      cmp RASTER_D012   
      beq Line251_2
      
      stx EXTCOL_D020
      rts

There are two versions main.asm and main2.asm - they both use similar approach, but are slightly different.

Note we call at the beginning of the program the SEI instruction, which disables maskable interrupts. We need to stop CIA interrupt (occur 50 times per second), because it is not synchronized with the screen rendering and its handling is long enough to miss the line start or end detection so if it occurs the red line is either not drawn or red color is not disabled, so it fills the whole background. The resulting effect is either line or background blinking.

The result:

Git: https://github.com/RetroAsmDev/C64AsmGameJourney/tree/master/line_opt