Browse code

Added nametable demo

Devine Lu Linvega authored on 16/11/2021 15:35:11
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,232 @@
1
+
2
+( 
3
+	uxnasm projects/examples/demos/nametable.tal bin/nametable.rom 
4
+	uxnemu bin/nametable.rom 
5
+)
6
+
7
+%+  { ADD } %-   { SUB }              %/   { DIV }  
8
+%<  { LTH } %>   { GTH }  %=  { EQU } %!   { NEQ } 
9
+%++ { ADD2 } %-- { SUB2 }              %// { DIV2 } 
10
+%<< { LTH2 } %>> { GTH2 } %== { EQU2 } %!! { NEQ2 }  
11
+
12
+%2** { #10 SFT2 } %2// { #01 SFT2 }
13
+%8** { #30 SFT2 } %8// { #03 SFT2 }
14
+%10** { #40 SFT2 } %10// { #04 SFT2 }
15
+%MOD2 { DIV2k MUL2 SUB2 }
16
+%MOD { DIVk MUL SUB }
17
+%RTN { JMP2r }
18
+
19
+%WIDTH { #0037 }
20
+%HEIGHT { #0029 } ( 08cf )
21
+%LENGTH { WIDTH HEIGHT MUL2 10** }
22
+
23
+( devices )
24
+
25
+|00 @System     [ &vector $2 &wst      $1 &rst    $1 &pad   $4 &r      $2 &g      $2 &b    $2 &debug  $1 &halt $1 ]
26
+|10 @Console    [ &vector $2 &read     $1 &pad    $5 &write $1 &error  $1 ]
27
+|20 @Screen     [ &vector $2 &width    $2 &height $2 &auto $1 &pad   $1 &x      $2 &y      $2 &addr $2 &pixel  $1 &sprite $1 ]
28
+|80 @Controller [ &vector $2 &button   $1 &key    $1 ]
29
+|90 @Mouse      [ &vector $2 &x        $2 &y      $2 &state $1 &wheel  $1 ]
30
+|a0 @File       [ &vector $2 &success  $2 &stat   $2 &delete $1 &append $1 &name  $2 &length $2 &read $2 &write $2 ]
31
+
32
+( variables )
33
+
34
+|0000
35
+
36
+( program )
37
+
38
+@tiles-frame
39
+	&x $2 &y $2
40
+@nametable-frame
41
+	&x $2 &y $2
42
+@buffer $10
43
+
44
+|0100 ( -> )
45
+
46
+	( theme ) 
47
+	#34cd .System/r DEO2 
48
+	#28ac .System/g DEO2 
49
+	#297b .System/b DEO2
50
+
51
+	WIDTH 8** #0010 ++ .Screen/width DEO2
52
+	#01e8 .Screen/height DEO2
53
+
54
+	#0008 .tiles-frame/x STZ2
55
+	HEIGHT 8** #0008 ++ .tiles-frame/y STZ2
56
+	#0008 .nametable-frame/x STZ2 
57
+	#0008 .nametable-frame/y STZ2
58
+
59
+	;input-name 
60
+		DUP2 ,load JSR
61
+		;assoc JSR2 
62
+
63
+	;draw-tiles JSR2
64
+	;draw-nametable JSR2
65
+	;draw-short JSR2
66
+
67
+BRK
68
+
69
+@load ( filename* -- )
70
+
71
+	LIT2r 0000
72
+	.File/name DEO2
73
+	&stream
74
+		#0010 .File/length DEO2
75
+		;buffer 
76
+			DUP2 .File/read DEO2
77
+			,find-tile JSR #ffff !! ,&skip JCN
78
+				;buffer ;add-tile JSR2
79
+				&skip
80
+		INC2r
81
+		.File/success DEI2 #0000 !! ,&stream JCN
82
+	STH2r #0001 -- ;result/tiles STA2
83
+
84
+RTN
85
+
86
+@find-tile ( addr* -- addr* )
87
+
88
+	STH2
89
+	;result/length LDA2 #0000
90
+	&loop
91
+		DUP2 10** ;result/data ++ STH2kr ;tiles-equal JSR2 #00 = ,&continue JCN
92
+			NIP2 POP2r RTN
93
+			&continue
94
+		INC2 GTH2k ,&loop JCN
95
+	POP2 POP2 POP2r
96
+	( default ) #ffff
97
+
98
+RTN
99
+
100
+@tiles-equal ( a* b* -- bool )
101
+
102
+	STH2
103
+	DUP2 #0010 ++ SWP2
104
+	&loop
105
+		LDAk LDAkr STHr = ,&continue JCN
106
+			POP2 POP2 POP2r #00 RTN
107
+			&continue
108
+		INC2r
109
+		INC2 GTH2k ,&loop JCN
110
+	POP2 POP2 POP2r #01
111
+
112
+RTN
113
+
114
+@add-tile ( addr* -- addr* )
115
+
116
+	STH2
117
+	#0010 #0000
118
+	&loop
119
+		( addr* ) DUP2 ;result/length LDA2 10** ;result/data ++ ++
120
+		( data ) OVR2 STH2kr ++ LDA
121
+		( order ) ROT ROT STA
122
+		INC2 GTH2k ,&loop JCN
123
+	POP2 POP2
124
+	POP2r
125
+	( incr ) ;result/length LDA2 INC2 ;result/length STA2
126
+
127
+RTN
128
+
129
+@assoc ( -- length )
130
+
131
+	LIT2r 0000
132
+	.File/name DEO2
133
+	&stream
134
+		#0010 .File/length DEO2
135
+		;buffer 
136
+			DUP2 .File/read DEO2
137
+			;find-tile JSR2 STH2kr SWP2 ;set-tile JSR2
138
+		INC2r
139
+		.File/success DEI2 #0000 !! ,&stream JCN
140
+	POP2r
141
+
142
+RTN
143
+
144
+( draw )
145
+
146
+@draw-tiles ( -- )
147
+
148
+	.tiles-frame/x LDZ2 .Screen/x DEO2
149
+	.tiles-frame/y LDZ2 .Screen/y DEO2
150
+	;result/data .Screen/addr DEO2
151
+
152
+	( width ) LITr 00
153
+	( auto x addr ) #05 .Screen/auto DEO
154
+	;result/data ;result/length LDA2 10** ++ ;result/data
155
+	&loop
156
+		STHkr WIDTH NIP MOD ,&continue JCN
157
+			.tiles-frame/x LDZ2 .Screen/x DEO2
158
+			.Screen/y DEI2 #0008 ++ .Screen/y DEO2
159
+			POPr LITr 00
160
+			&continue
161
+		INCr
162
+		#81 .Screen/sprite DEO
163
+		#0010 ++ GTH2k ,&loop JCN
164
+	POP2 POP2
165
+	( auto none ) #00 .Screen/auto DEO
166
+	POPr
167
+
168
+RTN
169
+
170
+@set-tile ( id* addr* -- )
171
+
172
+	SWP2 2** ;result/data ++ ;result/length LDA2 10** ++ STA2
173
+
174
+RTN
175
+
176
+@get-tile ( id* -- addr* )
177
+
178
+	2** ;result/length LDA2 10** ;result/data ++ ++ LDA2
179
+
180
+RTN
181
+
182
+@draw-nametable ( -- )
183
+
184
+	;result/tiles LDA2 #0000
185
+	&loop
186
+		DUP2 WIDTH MOD2 8** .nametable-frame/x LDZ2 ++ .Screen/x DEO2
187
+		DUP2 WIDTH DIV2 8** .nametable-frame/y LDZ2 ++ .Screen/y DEO2
188
+		DUP2 ;get-tile JSR2 10** ;result/data ++ .Screen/addr DEO2
189
+		#81 .Screen/sprite DEO
190
+		INC2 GTH2k ,&loop JCN
191
+	POP2 POP2
192
+
193
+RTN
194
+
195
+@draw-short ( -- )
196
+
197
+	#0008 .Screen/x DEO2
198
+	#0008 .Screen/y DEO2
199
+	#01 .Screen/auto DEO
200
+		;result/length LDA2 SWP 
201
+		( high ) ,&draw-byte JSR
202
+		( low ) ,&draw-byte JSR
203
+	#05 .Screen/auto DEO
204
+
205
+RTN
206
+
207
+	&draw-byte ( byte -- )
208
+		DUP
209
+		( high ) #04 SFT ,&draw-char JSR
210
+		( low ) #0f AND ,&draw-char JSR
211
+		RTN
212
+	&draw-char ( num -- )
213
+		#30 SFT #00 SWP ;font-hex ADD2 .Screen/addr DEO2
214
+		#01 .Screen/sprite DEO
215
+		RTN
216
+
217
+@input-name "projects/pictures/bulma37x29.chr $1
218
+
219
+@font-hex ( 0-F )
220
+	007c 8282 8282 827c 0030 1010 1010 1010
221
+	007c 8202 7c80 80fe 007c 8202 1c02 827c
222
+	000c 1424 4484 fe04 00fe 8080 7c02 827c
223
+	007c 8280 fc82 827c 007c 8202 1e02 0202
224
+	007c 8282 7c82 827c 007c 8282 7e02 827c
225
+	007c 8202 7e82 827e 00fc 8282 fc82 82fc
226
+	007c 8280 8080 827c 00fc 8282 8282 82fc
227
+	007c 8280 f080 827c 007c 8280 f080 8080
228
+
229
+@result
230
+	&length $2
231
+	&tiles $2
232
+	&data
0 233
new file mode 100644
1 234
Binary files /dev/null and b/projects/pictures/bulma37x29.chr differ