-
Notifications
You must be signed in to change notification settings - Fork 0
/
window.s
100 lines (92 loc) · 2.49 KB
/
window.s
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
97
98
99
100
.intel_syntax
.globl window_loop
.set SDL_INIT_VIDEO, 0x20
.set WIDTH, 640 # 64 blocs of 10 pixels each
.set HEIGHT, 320
.macro SET_COLOR code
xor %rax, %rax
mov %rdi, %r13
mov %rsi, \code
mov %rdx, \code
mov %rcx, \code
mov %r8, 0xFF
call SDL_SetRenderDrawColor
.endm
.text
window_loop:
## Init the video driver
mov %rdi, SDL_INIT_VIDEO
call SDL_Init
## Window will be stored in rsp, whereas renderer is in rsp+8
sub %rsp, 16
xor %rax, %rax
mov %rdi, WIDTH
mov %rsi, HEIGHT
xor %rdx, %rdx
lea %rcx, [%rsp]
lea %r8 , [%rsp+8]
call SDL_CreateWindowAndRenderer
## Store Window in R12, Renderer in R13 and DT counter in R14
## As their address is not needed anymore
mov %r12, [%rsp]
mov %r13, [%rsp+8]
xor %r14, %r14
## Test whether window is null or not
cmp %r12, 0
je error
## Set color to white
SET_COLOR 0xFF
## Init the CPU and main loop
call load_jumptable
mov WORD PTR [pc], 0x200
mov BYTE PTR [sp], 0xF
begin:
## Test events
xor %rax, %rax
lea %rdi, event
call SDL_PollEvent
cmp %rax, 0
je endevt
## Event.type is a 32-bit value
xor %rax, %rax
mov %eax, [event]
cmp %eax, 256 # SDL_QUIT = 256
je dtroy # if SDL_QUIT, break the loop
endevt:
## Execute an instruction of the CHIP-8 machine
call execute_cpu
## Should clean the screen here first with SDL_RenderClear
SET_COLOR 0x00
call SDL_RenderClear
SET_COLOR 0xFF
## Draw rectangles depending on the screen informations
mov %rdi, %r13
call render_screen
## Render window
mov %rdi, %r13
call SDL_RenderPresent
mov %rdi, 8
call SDL_Delay
## Increment DT if needed
cmp WORD PTR [dt], 0
je begin
inc %r14
and %r14, 0x1
jz begin
dec WORD PTR [dt]
jmp begin
## Destroy
dtroy: xor %rax, %rax
mov %rdi, %r12
call SDL_DestroyWindow
call SDL_Quit
return: add %rsp, 16
ret
error: xor %rax, %rax
lea %rdi, winerr
call printf
jmp return
.data
winerr: .asciz "Could not create window\n"
.bss
event: .space 64 # Sizeof(SDL_Event) = 56