forked from fox32-arch/fox32rom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw_tile.asm
96 lines (85 loc) · 2.2 KB
/
draw_tile.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
; generic tile drawing routines
const TILEMAP_POINTER: 0x01FFFFF0
const TILEMAP_WIDTH: 0x01FFFFF4
const TILEMAP_HEIGHT: 0x01FFFFF8
; set the current tilemap
; inputs:
; r0: pointer to tilemap data
; r1: tile width
; r2: tile height
; outputs:
; none
set_tilemap:
mov [TILEMAP_POINTER], r0
mov [TILEMAP_WIDTH], r1
mov [TILEMAP_HEIGHT], r2
ret
; get the current tilemap
; inputs:
; none
; outputs:
; r0: pointer to tilemap data
; r1: tile width
; r2: tile height
get_tilemap:
mov r0, [TILEMAP_POINTER]
mov r1, [TILEMAP_WIDTH]
mov r2, [TILEMAP_HEIGHT]
ret
; draw a single tile to a framebuffer
; inputs:
; r0: tile number
; r1: X coordinate
; r2: Y coordinate
; r8: pointer to framebuffer
; r9: framebuffer width (pixels)
; outputs:
; none
draw_tile_generic:
push r0
push r1
push r2
push r5
push r6
push r7
push r8
push r9
; calculate pointer to the tile data
mov r6, [TILEMAP_WIDTH]
mov r7, [TILEMAP_HEIGHT]
push r6
mul r6, r7
mul r0, r6
mul r0, 4 ; 4 bytes per pixel
add r0, [TILEMAP_POINTER] ; r0: pointer to tile data
pop r6
; calculate pointer to the framebuffer
mul r9, 4 ; 4 bytes per pixel
mul r2, r9 ; y * width * 4
mul r1, 4 ; x * 4
add r1, r2 ; y * width * 4 + (x * 4)
add r1, r8 ; r1: pointer to framebuffer
; r8: tile width in bytes
mov r8, r6
mul r8, 4
draw_tile_generic_y_loop:
mov r5, r6 ; x counter
draw_tile_generic_x_loop:
mov [r1], [r0] ; draw pixel
add r0, 4 ; increment tile pointer
add r1, 4 ; increment framebuffer pointer
dec r5
ifnz jmp draw_tile_generic_x_loop ; loop if there are still more X pixels to draw
sub r1, r8 ; return to the beginning of this line
add r1, r9 ; increment to the next line by adding the framebuffer width in bytes
dec r7 ; decrement height counter
ifnz jmp draw_tile_generic_y_loop ; loop if there are still more Y pixels to draw
pop r9
pop r8
pop r7
pop r6
pop r5
pop r2
pop r1
pop r0
ret