aboutsummaryrefslogtreecommitdiff
path: root/example.asm
blob: 7584074386f9e57afefcfbb9bd896d6c7ace4f58 (plain)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
;
; example.asm for TR8
;
.org 0

; address of the frame interrupt vector
.equ INT_VECT 0xff00

    ; setup an int handler so we
    ; can use the frame int to sync
    ld a, >INT_VECT
    ld x, <INT_VECT
    ld b, <int_handler
    ld [a : x], b
    inc x
    ld b, >int_handler
    ld [a : x], b

    ; enable interrupts
    cif

    ld b, 0
loop:
    call fill_screen
    push b

    ; update sprite position

    ld a, >incx
    ld x, <incx
    ld y, [a : x]
    ld a, >px
    ld x, <px
    ld b, [a : x]
    ; px + incx
    add b, y
    ; 128 - 16
    cmp b, 112
    bnc
    jmp turn_x

    ; new x
    ld [a : x], b
    jmp update_y

turn_x:
    ld a, >incx
    ld x, <incx
    call turn

update_y:
    ld a, >incy
    ld x, <incy
    ld y, [a : x]
    ld a, >py
    ld x, <py
    ld b, [a : x]
    ; py + incy
    add b, y
    ; 128 - 16
    cmp b, 112
    bnc
    jmp turn_y

    ; new y
    ld [a : x], b
    jmp update_done

turn_y:
    ld a, >incy
    ld x, <incy
    call turn

update_done:

    call draw_sprite

    ; wait 1 frame
    sif
    halt

    pop b
    jmp loop

    ; make the sprite change direction
    ; in: [a : x] inc*
turn:
    ;  1 -> -1
    ; -1 -> 1
    ld y, [a : x]
    ld b, 254
    xor y, b
    ld [a : x], y

    ; change color
    ld b, [sp + 2]
    inc b
    and b, 15
    ld [sp + 2], b
    ret


    ; fill frame-buffer
    ; in: reg b color
fill_screen:
    ld a, 0xbf
    ld x, 0
    ld y, 0x40
fill_loop:
    ld [a : x], b
    inc x
    bno
    jmp fill_loop
    inc a
    dec y
    bnz
    jmp fill_loop
    ret

int_handler:
    iret

    ; draw sprite
    ; in: coordinates in px, py
draw_sprite:
    ; blitter in settings mode
    ld x, 128
    ld a, 0xb0
    port a, x

    ; setup
    inc a
    ; source
    ld x, <sprite
    port a, x
    ld x, >sprite
    port a, x
    ; destination
    ld y, <px
    ld b, >px
    ld x, [b : y]
    port a, x
    ld y, <py
    ld b, >py
    ld x, [b : y]
    port a, x
    ; size 16x16
    ld x, 16
    port a, x
    port a, x

    ; now blit
    dec a
    ld x, 3
    ; 3: write with transparent color support
    port a, x
    ret

sprite:
    .incpng "assets/icon.png"

incx:
    .db 1
px:
    .db 18
incy:
    .db 1
py:
    .db 64