;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; SX VGA - Copyright 2008 by Michael Kohn
; http://www.mikekohn.net/
; mike ta mikekohn.net
;
; print a colorful word on the screen in colors!
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;   8 pixels front porch
;  96 pixels horizontal sync
;  40 pixels back porch
;   8 pixels left border
; 640 pixels video
;   8 pixels right border
; 800 pixels total per line

;   2 lines front porch
;   2 lines vertical sync
;  25 lines back porch
;   8 lines top border
; 480 lines video
;   8 lines bottom border
; 525 lines total per field

vertl equ $10
verth equ $11
vsync equ $12
red equ $13
green equ $14
blue equ $15
count equ $16
rgb equ $17
rgb_mask equ $18

DEVICE SX28, OSCHS3, TURBO
FREQ 50000000
IRC_CAL IRC_FAST
RESET main
RADIX DEC

org 0

main:

  mov !option, #%11010011
  mov w, #0
  mov !ra, w
  mov !rb, w      ; portb will control the VGA

  mov ra, w       ; set port A to 0 so the LED turns on there

  mov vertl, w
  mov verth, w
  mov vsync, w

  mov w, #7
  mov rgb_mask, w

start_scan:

horiz_scan:

  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ; 8 pixels front porch  (0)
  clr rtcc

  ; mov w, #%10000
  mov w, vsync
  mov rb, w   ; turn off RGB and hsync

  nop  ; 1
  nop  ; 2
  nop  ; 3
  nop  ; 4
  nop  ; 5
  nop  ; 6
  nop  ; 7
  nop  ; 8
  nop  ; 9
  nop  ; 10
  nop  ; 11
  nop  ; 12
  nop  ; 13
  nop  ; 14

  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ; 96 pixels hsync       (1)

  setb rb.3   ; turn on hsync

  ; increment vertical scan counter
  incsz vertl
  jmp vertl_not_zero
  inc verth
vertl_not_zero:

  ; check if we are at the end of the sceren and should reset vscan counter
  cse verth, #2
  jmp not_end_of_screen
  cse vertl, #$0d
  jmp not_end_of_screen
  clr verth
  clr vertl

not_end_of_screen:

  ; check if we should turn on vsync
  cse verth, #0
  jmp leave_sync_check
  cse vertl, #2
  jmp vert_not_2
  mov w, #%10000
  mov vsync, w
  jmp leave_sync_check
vert_not_2:
  cse vertl, #4
  jmp leave_sync_check
  clr vsync

leave_sync_check:


hsync_wait:
  cjb rtcc, #13, hsync_wait

  ; 40 pixels back porch and 8 pixels left border (13)

  clrb rb.3   ; turn off hsync

hback_porch_wait:
  cjb rtcc, #19, hback_porch_wait


  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ; 640 pixels video      (19)


  ; set up playfield.. this could go in the hsync section or maybe the back porch..
  ; but i don't give a crap

  ; 244 187 224
  ; 132 144 128
  ; 247 144 128
  ; 20 144 128
  ; 244 184 128

  cjne verth, #1, dont_draw
  cja vertl, #8, line2
  mov red, #244
  mov green, #187
  mov blue, #224
  jmp done_with_playfield_setup
line2:
  cja vertl, #16, line3
  mov red, #132
  mov green, #144
  mov blue, #128
  jmp done_with_playfield_setup
line3:
  cja vertl, #24, line4
  mov red, #247
  mov green, #144
  mov blue, #128
  jmp done_with_playfield_setup
line4:
  cja vertl, #32, line5
  mov red, #20
  mov green, #144
  mov blue, #128
  jmp done_with_playfield_setup
line5:
  cja vertl, #40, dont_draw
  mov red, #244
  mov green, #184
  mov blue, #128
  jmp done_with_playfield_setup

dont_draw:
  clr red
  clr green
  clr blue
  ; jmp hsync_wait

  mov rgb, vsync

  ; just sync up a bit
done_with_playfield_setup:
  cjb rtcc, #44, done_with_playfield_setup


  ;cse verth, #1
  ;jmp not_in_range
  ;cse vertl, #10
  ;jmp not_in_range
  ;setb rb.0

;not_in_range:

  ;jmp playfield_wait


  mov count, #128
repeat_red:
  and rgb, #%11111000
  mov w, count
  and w, red
  sz
  jmp red_on
  nop
  clrb rgb.0
  jmp done_red
red_on:
  nop
  nop
  setb rgb.0
done_red:
  mov rb, rgb
  clc
  rr count
  test count
  sz
  jmp repeat_red


  mov count, #128
repeat_green:
  and rgb, #%11111000
  mov w, count
  and w, green
  sz
  jmp green_on
  nop
  clrb rgb.1
  jmp done_green
green_on:
  nop
  nop
  setb rgb.1
done_green:
  mov rb, rgb
  clc
  rr count
  test count
  sz
  jmp repeat_green

  mov count, #128
repeat_blue:
  and rgb, #%11111000
  mov w, count
  and w, blue
  sz
  jmp blue_on
  nop
  clrb rgb.2
  jmp done_blue
blue_on:
  nop
  nop
  setb rgb.2
done_blue:
  mov rb, rgb
  clc
  rr count
  test count
  sz
  jmp repeat_blue

  clrb rb.2


playfield_wait:
  cjb rtcc, #99, playfield_wait

  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ; 8 pixels right border (99)

  mov w, #%10000
  and rb, w   ; turn off RGB and hsync

  ; 2 cycles above.. 3 cycles for the jmp.. 1 cycle to clear the counter
  ; so we need 10 more cycles to be 8 pixels wide

  nop ; 1
  nop ; 2
  nop ; 3
  nop ; 4
  nop ; 5
  nop ; 6
  nop ; 7
  nop ; 8
  nop ; 9
  nop ; 10

;border_wait:
;  cjb rtcc, #100, border_wait


  jmp start_scan

