From 6e7809848bca39df7c1d9362baf56c779b9508e8 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sun, 3 Aug 2025 22:01:36 -0400 Subject: [PATCH] hello gp routines --- code/graphics_system/dsl.s | 9 +++ code/graphics_system/gp.s | 40 +++++++++++ code/graphics_system/hello_gouraud.s | 11 ++- code/graphics_system/hello_gp_routines.s | 73 +++++++++++++++++++ code/graphics_system/hellogpu_instructors.s | 78 --------------------- scripts/build.ps1 | 15 ++-- 6 files changed, 135 insertions(+), 91 deletions(-) create mode 100644 code/graphics_system/hello_gp_routines.s delete mode 100644 code/graphics_system/hellogpu_instructors.s diff --git a/code/graphics_system/dsl.s b/code/graphics_system/dsl.s index 4ab7940..d132308 100644 --- a/code/graphics_system/dsl.s +++ b/code/graphics_system/dsl.s @@ -32,6 +32,15 @@ rtmp_1 equ $t1 rtmp_2 equ $t2 rtmp_3 equ $t3 rtmp_4 equ $t4 +; Static Variables +rstatic_0 equ $s0 +rstatic_1 equ $s1 +rstatic_2 equ $s2 +rstatic_3 equ $s3 +rstatic_4 equ $s4 +rstatic_5 equ $s5 +rstatic_6 equ $s6 +rstatic_7 equ $s7 ; Subroutine arguments rarg_0 equ $a0 rarg_1 equ $a1 diff --git a/code/graphics_system/gp.s b/code/graphics_system/gp.s index 7448eb6..669f931 100644 --- a/code/graphics_system/gp.s +++ b/code/graphics_system/gp.s @@ -127,3 +127,43 @@ gp_b16_Y equ 16 load_imm reg_scratch, cmd store_word reg_scratch, port .endmacro + +.org 0x80010000 + 2000 + +.func gp_draw_tri_flat ;( + @@io_offset equ rarg_0 + @@color equ rarg_1 + @@vert_1 equ rarg_2 + @@vert_2 equ rarg_3 + @@vert_3 equ rstatic_0 +;) + @@cmd equ rtmp_2 + load_imm @@cmd, gp_Polygon + or @@cmd, @@cmd, @@color + store_word @@cmd, gpio_port0(@@io_offset) + store_word @@vert_1, gpio_port0(@@io_offset) + store_word @@vert_2, gpio_port0(@@io_offset) + store_word @@vert_3, gpio_port0(@@io_offset) + jump_reg rret_addr :: nop +.endfunc + +.func gp_draw_tri_grouand ;( + @@io_offset equ rarg_0 + @@color equ rarg_1 + @@color_2 equ rstatic_1 + @@color_3 equ rstatic_2 + @@vert_1 equ rarg_2 + @@vert_2 equ rarg_3 + @@vert_3 equ rstatic_0 +;) + @@cmd equ rtmp_2 + load_imm @@cmd, gp_Polygon | gp_Poly_ShadeGourand + or @@cmd, @@cmd, @@color + store_word @@cmd, gpio_port0(@@io_offset) + store_word @@vert_1, gpio_port0(@@io_offset) + store_word @@color_2, gpio_port0(@@io_offset) + store_word @@vert_2, gpio_port0(@@io_offset) + store_word @@color_3, gpio_port0(@@io_offset) + store_word @@vert_3, gpio_port0(@@io_offset) + jump_reg rret_addr :: nop +.endfunc diff --git a/code/graphics_system/hello_gouraud.s b/code/graphics_system/hello_gouraud.s index 1c8d05e..bc1e0c0 100644 --- a/code/graphics_system/hello_gouraud.s +++ b/code/graphics_system/hello_gouraud.s @@ -27,11 +27,11 @@ main: gp1 equ gpio_port1(reg_io_offset) ; Setup Display Control - gcmd_push gp0, rtmp_1, gp_Reset - gcmd_push gp0, rtmp_1, gp_DisplayEnabled - gcmd_push gp0, rtmp_1, gp_DisplayMode_320x240_15bit_NTSC - gcmd_push gp0, rtmp_1, gp_HorizontalDisplayRange_3168_608 - gcmd_push gp0, rtmp_1, gp_VerticalDisplayRange_264_24 + 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 @@ -67,4 +67,3 @@ idle: jump idle :: nop .close - diff --git a/code/graphics_system/hello_gp_routines.s b/code/graphics_system/hello_gp_routines.s new file mode 100644 index 0000000..f8ea47a --- /dev/null +++ b/code/graphics_system/hello_gp_routines.s @@ -0,0 +1,73 @@ +.psx +.create "./build/hello_gp_routines.bin", 0x80010000 + +.include "./code/graphics_system/dsl.s" +.include "./code/graphics_system/gp.s" + +; Entry Point of Code +.org 0x80010000 + +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 320 +Display_Height equ 239 +Display_HalfWidth equ 320 / 2 +Display_HalfHeight equ 240 / 2 + +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 + 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 + move rarg_0, reg_io_offset ; arg 1 for gp_draw_tri_flat + load_imm rarg_1, Color_PS_GoldenPoppy + load_imm rarg_2, 100 * -1 + Display_HalfHeight << gp_b16_Y | -100 + Display_HalfWidth << gp_b16_X + load_imm rarg_3, 20 * -1 + Display_HalfHeight << gp_b16_Y | 20 + Display_HalfWidth << gp_b16_X + load_imm rstatic_0, 50 * -1 + Display_HalfHeight << gp_b16_Y | 30 + Display_HalfWidth << gp_b16_X + 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 + load_imm rarg_3, 0 * -1 + Display_HalfHeight << gp_b16_Y | 20 + Display_HalfWidth << gp_b16_X + load_imm rstatic_0, -100 * -1 + Display_HalfHeight << gp_b16_Y | 30 + Display_HalfWidth << gp_b16_X + jump_nlink gp_draw_tri_flat :: nop +; 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_grouand :: nop + +idle: + jump idle :: nop + +.close diff --git a/code/graphics_system/hellogpu_instructors.s b/code/graphics_system/hellogpu_instructors.s deleted file mode 100644 index 64d6516..0000000 --- a/code/graphics_system/hellogpu_instructors.s +++ /dev/null @@ -1,78 +0,0 @@ -.psx -.create "./build/hellogpu_instructors.bin", 0x80010000 - -.org 0x80010000 - -; --------------------- -; IO Port -; --------------------- -IO_BASE_ADDR equ 0x1F80 ; IO Ports Memory map base address - -; --------------------- -; GPU Registers -; --------------------- -GP0 equ 0x1810 ; GP0 @ $1F801810: Rendering data & VRAM Access -GP1 equ 0x1814 ; GP1 @ $1F801814: Display Control & Environment setup - -Main: - lui $t0, IO_BASE_ADDR ; t0 = I/O Port Base Address (mapped at 0x1F80****) - - ; --------------------------------------------------------------------------- - ; Send commands to GP1 (mapped at 0x1F801814) - ; These GP1 is for display control and environment setup - ; (Command = 8-Bit MSB, Parameter = 24-Bit LSB) - ; CCPPPPPP: CC=Command PPPPPP=Parameter - ; --------------------------------------------------------------------------- - li $t1, 0x00000000 ; 00 = Reset GPU - sw $t1, GP1($t0) ; Write to GP1 - - li $t1, 0x03000000 ; 03 = Display enable - sw $t1, GP1($t0) ; Write to GP1 - - li $t1, 0x08000001 ; 08 = Display mode (320x240, 15-bit, NTSC) - sw $t1, GP1($t0) ; Write to GP1 - - li $t1, 0x06C60260 ; 06 = Horz Display Range - 0bxxxxxxxxxxXXXXXXXXXX (3168..608) - sw $t1, GP1($t0) ; Write to GP1 - - li $t1, 0x07042018 ; 07 = Vert Display Range - 0byyyyyyyyyyYYYYYYYYYY (264..24) - sw $t1, GP1($t0) ; Write to GP1 - - ; --------------------------------------------------------------------------- - ; Send commands to GP0 (mapped at 0x1F801810) - ; These GP0 commands are to setup the drawing area - ; (Command = 8-Bit MSB, Parameter = 24-Bit LSB) - ; CCPPPPPP CC=Command PPPPPP=Parameter - ; --------------------------------------------------------------------------- - li $t1, 0xE1000400 ; E1 = Draw Mode Settings - sw $t1, GP0($t0) ; Write to GP0 - - li $t1, 0xE3000000 ; E3 = Drawing Area TopLeft - 0bYYYYYYYYYYXXXXXXXXXX (10 bits for Y and X) - sw $t1, GP0($t0) ; Write to GP0 - - li $t1, 0xE403BD3F ; E4 = Drawing area BottomRight - 0bYYYYYYYYYYXXXXXXXXXX (10 bits for X=319 and Y=239) - sw $t1, GP0($t0) ; Write to GP0 - - li $t1, 0xE5000000 ; E5 = Drawing Offset - 0bYYYYYYYYYYYXXXXXXXXXXXX (X=0, Y=0) - sw $t1, GP0($t0) ; Write to GP0 - - ; --------------------------------------------------------------------------- - ; Clear the screen (draw a rectangle on VRAM). - ; --------------------------------------------------------------------------- - li $t1, 0x020000FF ; 02 = Fill Rectancle in VRAM (Parameter Color: 0xBBGGRR) - sw $t1, GP0($t0) ; Write GP0 Command - li $t1, 0x00000000 ; Fill Area, Parameter: 0xYYYYXXXX - Topleft (0,0) - sw $t1, GP0($t0) ; Write to GP0 - - li $t1, 0x00EF013F ; Fill Area, 0xHHHHWWWW (Height=239, Width=319) - sw $t1, GP0($t0) ; Write to GP0 - - ; --------------------------------------------------------------------------- - ; TODO: Draw a flat-shaded triangle - ; --------------------------------------------------------------------------- - -LoopForever: - j LoopForever ; Continuous loop - nop - -.close diff --git a/scripts/build.ps1 b/scripts/build.ps1 index 3673626..b4195c5 100644 --- a/scripts/build.ps1 +++ b/scripts/build.ps1 @@ -32,10 +32,11 @@ function build-program { param( write-host 'Done!' pop-location } -build-program 'fillmem' 'fillmem' -build-program 'warmup' 'exercise_1' -build-program 'warmup' 'exercise_2' -build-program 'warmup' 'exercise_3' -build-program 'factorial' 'factorial' -build-program 'graphics_system' 'hellogpu' -build-program 'graphics_system' 'hello_gouraud' +# build-program 'fillmem' 'fillmem' +# build-program 'warmup' 'exercise_1' +# build-program 'warmup' 'exercise_2' +# build-program 'warmup' 'exercise_3' +# build-program 'factorial' 'factorial' +# build-program 'graphics_system' 'hellogpu' +# build-program 'graphics_system' 'hello_gouraud' +build-program 'graphics_system' 'hello_gp_routines'