.psx
.create "./build/hello_image.bin", 0x80010000

.include "./code/graphics_hello/dsl.s"
.include "./code/graphics_hello/gp.s"

Color_RedFF           equ 0x0000FF
Color_22              equ 0x222222
Color_PS_CadmiumRed   equ 0x2400DF
Color_PS_CelticBlue   equ 0x723F00
Color_PS_GoldenPoppy  equ 0x00C3F3
Color_PS_PersianGreen equ 0x9FAC00

Display_Width      equ 319
Display_Height     equ 239
Display_HalfWidth  equ 319 / 2
Display_HalfHeight equ 239 / 2


.org 0x80010000 + 4000

Image_SizeX    equ 32
Image_SizeY    equ 32
Image_ByteSize equ Image_SizeX * Image_SizeY * gp_pixel16

Image:
  ; 32 * 32 * 2 = 2048 bytes 
  .hword 0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60
  .hword 0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0AFF,0x0AFF,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60
  .hword 0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60
  .hword 0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60
  .hword 0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60
  .hword 0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60
  .hword 0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60
  .hword 0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60
  .hword 0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0AFF,0x0AF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x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x7500,0x7500,0x7500,0x7500,0x7500,0x7500,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0A60,0x0A60,0x0A60
  .hword 0x0A60,0x0A60,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x7500,0x7500,0x7500,0x7500,0x7500,0x7500,0x7500,0x7500,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0A60,0x0A60
  .hword 0x0A60,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x7500,0x7500,0x7500,0x7500,0x7500,0x7500,0x7500,0x7500,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0A60
  .hword 0x0A60,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x7500,0x7500,0x7500,0x7500,0x7500,0x7500,0x7500,0x7500,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0A60
  .hword 0x0A60,0x0A60,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x7500,0x7500,0x7500,0x7500,0x7500,0x7500,0x7500,0x7500,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0A60,0x0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0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60
  .hword 0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60
  .hword 0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60
  .hword 0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60
  .hword 0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60
  .hword 0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60
  .hword 0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60
  .hword 0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0AFF,0x0AFF,0x0AFF,0x0AFF,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60
  .hword 0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0AFF,0x0AFF,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60
  .hword 0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60,0x0A60

; Entry Point of Code
.org 0x80010000
main:
	reg_io_offset equ rtmp_0
	load_uimm rtmp_0, IO_BASE_ADDR

	gp0 equ gpio_port0(reg_io_offset)
	gp1 equ gpio_port1(reg_io_offset)

; Setup Display Control
	gcmd_push gp1, rtmp_1, gp_Reset
	gcmd_push gp1, rtmp_1, gp_DisplayEnabled
	gcmd_push gp1, rtmp_1, gp_DisplayMode_320x240_15bit_NTSC
	gcmd_push gp1, rtmp_1, gp_HorizontalDisplayRange_3168_608
	gcmd_push gp1, rtmp_1, gp_VerticalDisplayRange_264_24
	gcmd_push gp0, rtmp_1, gp_ModeSetting_DipArea
	gcmd_push gp0, rtmp_1, 		gp_SetArea_TopLeft     | 0              << gp_b10_Y | 0             << gp_b10_X
	gcmd_push gp0, rtmp_1, 		gp_SetArea_BottomRight | Display_Height << gp_b10_Y | Display_Width << gp_b10_X
	gcmd_push gp0, rtmp_1, 		gp_SetOffset           | 0              << gp_b10_Y | 0             << gp_b10_X
; Clear the screen
	gcmd_push gp0, rtmp_1, gp_RectFillVM | Color_22
	gcmd_push gp0, rtmp_1, 		0              << gp_b16_Y | 0             << gp_b16_X
	gcmd_push gp0, rtmp_1, 		Display_Height << gp_b16_Y | Display_Width << gp_b16_X

; Draw a flat-shaded quad
	gcmd_push gp0, rtmp_1, gp_Quad | Color_PS_CelticBlue
	gcmd_push gp0, rtmp_1, 		 15 * -1 + Display_HalfHeight << gp_b16_Y |    0 + Display_HalfWidth << gp_b16_X
	gcmd_push gp0, rtmp_1, 		 24 * -1 + Display_HalfHeight << gp_b16_Y |  100 + Display_HalfWidth << gp_b16_X
	gcmd_push gp0, rtmp_1, 		-30 * -1 + Display_HalfHeight << gp_b16_Y | -100 + Display_HalfWidth << gp_b16_X
	gcmd_push gp0, rtmp_1, 		-50 * -1 + Display_HalfHeight << gp_b16_Y |   55 + Display_HalfWidth << gp_b16_X
; Draw a flat-shaded triangle
	stack_alloc gp_draw_tri_flat__sp_size ; (used for following call)
		move     rarg_0, reg_io_offset      ; (used for following call)
		load_imm rarg_1, Color_PS_GoldenPoppy
		load_imm rarg_2, 100 * -1 + Display_HalfHeight << gp_b16_Y | -100 + Display_HalfWidth << gp_b16_X :: store_word rarg_2, 0 * gp_vec2($sp)
		load_imm rarg_2,  20 * -1 + Display_HalfHeight << gp_b16_Y |   20 + Display_HalfWidth << gp_b16_X :: store_word rarg_2, 1 * gp_vec2($sp)
		load_imm rarg_2,  50 * -1 + Display_HalfHeight << gp_b16_Y |   30 + Display_HalfWidth << gp_b16_X :: store_word rarg_2, 2 * gp_vec2($sp)
	jump_nlink gp_draw_tri_flat :: nop
; Bonus traingle
		load_imm rarg_1,  Color_PS_CadmiumRed
		load_imm rarg_2,   50 * -1 + Display_HalfHeight << gp_b16_Y | -100 + Display_HalfWidth << gp_b16_X :: store_word rarg_2, 0 * gp_vec2($sp)
		load_imm rarg_2,    0 * -1 + Display_HalfHeight << gp_b16_Y |   20 + Display_HalfWidth << gp_b16_X :: store_word rarg_2, 1 * gp_vec2($sp)
		load_imm rarg_2, -100 * -1 + Display_HalfHeight << gp_b16_Y |   30 + Display_HalfWidth << gp_b16_X :: store_word rarg_2, 2 * gp_vec2($sp)
	jump_nlink gp_draw_tri_flat :: nop
	stack_release gp_draw_tri_flat__sp_size
; Gourand shaded triangle
		load_imm rarg_1,     Color_PS_PersianGreen
		load_imm rstatic_1,  Color_PS_GoldenPoppy
		load_imm rstatic_2,  Color_PS_CadmiumRed
		load_imm rarg_2,     -35 * -1 + Display_HalfHeight << gp_b16_Y | 145 + Display_HalfWidth << gp_b16_X
		load_imm rarg_3,       0 * -1 + Display_HalfHeight << gp_b16_Y |  50 + Display_HalfWidth << gp_b16_X
		load_imm rstatic_0,   40 * -1 + Display_HalfHeight << gp_b16_Y |  60 + Display_HalfWidth << gp_b16_X
	jump_nlink gp_draw_tri_gouraud :: nop

; Copy image contents to vram
	gcmd_push gp0, rtmp_1, gp_Blit_CPU_VM
	gcmd_push gp0, rtmp_1, 		0 << gp_b16_Y | Display_Width << gp_b16_X
	gcmd_push gp0, rtmp_1, 		Image_SizeY << gp_b16_Y | Image_SizeX << gp_b16_X
	; DMA commands
	@id         equ rtmp_2
	@img_cursor equ rtmp_3
		load_imm @id, Image_ByteSize
		shift_rl @id, @id, (word / 2)
		load_addr @img_cursor, Image
	loop_dma: 
		load_word  rtmp_1, (@img_cursor) :: nop :: store_word rtmp_1, gp0 ; @img_curor -> gp_dma_cpu_vm(word)
		add_si @img_cursor, @img_cursor, word                             ; @img_cursor ++
	add_ui @id, @id, -1 :: branch_ne_zero @id, loop_dma :: nop

idle:
	jump idle :: nop

.close
