-
Notifications
You must be signed in to change notification settings - Fork 1
/
VGABitmap8BPP-Demo.spin2
498 lines (371 loc) · 12.2 KB
/
VGABitmap8BPP-Demo.spin2
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
{
----------------------------------------------------------------------------------------------------
Filename: VGAbitmap8BPP-Demo.spin2
Description: Demo of the 8bpp VGA bitmap driver
Author: Jesse Burt
Started: Apr 6, 2020
Updated: Sep 6, 2024
Copyright (c) 2024 - See end of file for terms of use.
----------------------------------------------------------------------------------------------------
}
CON
_clkfreq = 180_000_000
_xtlfreq = 20_000_000
VAR
long _stack_timer[50]
long _timer_set
byte _timer_cog
OBJ
fnt: "font.5x8"
math: "math.int"
ser: "com.serial.terminal.ansi" | SER_BAUD=2_000_000
vga: "display.vga.bitmap-8bpp" | BASEPIN=24, VF=10, VB=33, HF=16, HS=96, HB=48, HV=640
' NOTE: Timings can also be changed with set_timings() (before calling driver start() )
PUB main() | time_ms
setup()
vga.clear()
demo_greet()
waitms(5_000)
vga.clear()
time_ms := 5_000
ser.pos_xy(0, 3)
demo_shadebobs(time_ms)
demo_sinewave(time_ms)
vga.clear()
demo_tri_wave(time_ms)
vga.clear()
demo_mem_scroller(time_ms, $0000, $7_FFFF)
vga.clear()
demo_bitmap(time_ms, $1_0000)
vga.clear()
demo_box(time_ms)
vga.clear()
demo_box_filled(time_ms)
vga.clear()
demo_linesweep_x(time_ms)
vga.clear()
demo_linesweep_y(time_ms)
vga.clear()
demo_line(time_ms)
vga.clear()
demo_plot(time_ms)
vga.clear()
demo_bouncing_ball(time_ms, 5)
vga.clear()
demo_circle(time_ms)
vga.clear()
demo_wander(time_ms)
vga.clear()
demo_seq_text(time_ms)
vga.clear()
demo_rnd_text(time_ms)
waitms(2000)
vga.clear()
repeat
PUB demo_bouncing_ball(testtime, radius) | iteration, bx, by, dx, dy
' Draws a simple ball bouncing off screen edges
bx := (math.rndi(XMAX) // (WIDTH - radius * 4)) + radius * 2 'Pick a random screen location to
by := (math.rndi(YMAX) // (HEIGHT - radius * 4)) + radius * 2 ' start from
dx := math.rndi(4) // 2 * 2 - 1 'Pick a random direction to
dy := math.rndi(4) // 2 * 2 - 1 ' start moving
ser.str(@"demo_bouncing_ball - ")
_timer_set := testtime
iteration := 0
repeat while _timer_set
bx += dx
by += dy
if ((by <= radius) OR (by >= YMAX - radius)) ' If we reach the top or bottom of the
dy *= -1 ' screen, change direction
if ((bx <= radius) OR (bx >= XMAX - radius)) ' Same with the left or right sides
dx *= -1
vga.circle(bx, by, radius, vga#MAX_COLOR, false)
vga.wait_vsync()
iteration++
vga.clear()
report(testtime, iteration)
PUB demo_bitmap(testtime, bitmap_addr) | iteration
' Continuously redraws bitmap at address bitmap_addr
ser.str(@"demo_bitmap - ")
_timer_set := testtime
iteration := 0
repeat while _timer_set
vga.bitmap(bitmap_addr, 0, 0, XMAX, YMAX)
iteration++
report(testtime, iteration)
PUB demo_box(testtime) | iteration, c
' Draws random boxes
ser.str(@"demo_box - ")
_timer_set := testtime
iteration := 0
repeat while _timer_set
c := math.rndi(vga.MAX_COLOR)
vga.box(math.rndi(XMAX), math.rndi(YMAX), math.rndi(XMAX), math.rndi(YMAX), c, FALSE)
iteration++
report(testtime, iteration)
PUB demo_box_filled(testtime) | iteration, c
' Draws random filled boxes
ser.str(@"demo_box_filled - ")
_timer_set := testtime
iteration := 0
repeat while _timer_set
c := math.rndi(vga.MAX_COLOR)
vga.box(math.rndi(XMAX), math.rndi(YMAX), math.rndi(XMAX), math.rndi(YMAX), c, TRUE)
iteration++
report(testtime, iteration)
PUB demo_circle(testtime) | iteration, x, y, r, c
' Draws circles at random locations
ser.str(@"demo_circle - ")
_timer_set := testtime
iteration := 0
repeat while _timer_set
x := math.rndi(XMAX)
y := math.rndi(YMAX)
r := math.rndi(YMAX/2)
c := math.rndi(vga.MAX_COLOR)
vga.circle(x, y, r, c, false)
iteration++
report(testtime, iteration)
PUB demo_greet()
' Display the banner/greeting
vga.fgcolor(vga.MAX_COLOR)
vga.bgcolor(0)
vga.pos_xy(0, 1)
vga.printf("VGA 8bpp on the Parallax P2X8C4M64P @ %dMHz\n\r", clkfreq/1_000_000)
vga.printf("%dx%d", WIDTH, HEIGHT)
PUB demo_line(testtime) | iteration, c
' Draws random lines
ser.printf("demo_line - ")
_timer_set := testtime
iteration := 0
repeat while _timer_set
c := math.rndi(vga.MAX_COLOR)
vga.line(math.rndi(XMAX), math.rndi(YMAX), math.rndi(XMAX), math.rndi(YMAX), c)
iteration++
report(testtime, iteration)
PUB demo_linesweep_x(testtime) | iteration, x, c
' Draws lines sweeping from left-right to the opposite diagonal direction
x := 0
ser.printf("demo_linesweep_x - ")
_timer_set := testtime
iteration := 0
repeat while _timer_set
x++
if (x > XMAX)
x := 0
c++
if (c > vga.MAX_COLOR)
c := 0
vga.line(x, 0, XMAX-x, YMAX, c)
iteration++
report(testtime, iteration)
PUB demo_linesweep_y(testtime) | iteration, y, c
' Draws lines sweeping from top-down to the opposite diagonal direction
y := 0
ser.printf("demo_linesweep_y - ")
_timer_set := testtime
iteration := 0
repeat while _timer_set
y++
if (y > YMAX)
y := 0
c++
if (c > vga.MAX_COLOR)
c := 0
vga.line(XMAX, y, 0, YMAX-y, c)
iteration++
report(testtime, iteration)
PUB demo_mem_scroller(testtime, start_addr, end_addr) | iteration, pos, st, en
' Dumps Propeller Hub RAM (and/or ROM) to the display buffer
pos := start_addr
ser.printf("demo_mem_scroller - ")
_timer_set := testtime
iteration := 0
repeat while _timer_set
pos += BPL
if (pos > end_addr)
pos := start_addr
vga.bitmap(pos, 0, 0, XMAX, YMAX)
iteration++
report(testtime, iteration)
PUB demo_plot(testtime) | iteration, x, y, c
' Draws random pixels to the screen
ser.printf("demo_plot - ")
_timer_set := testtime
iteration := 0
repeat while _timer_set
c := math.rndi(vga.MAX_COLOR)
vga.plot(math.rndi(XMAX), math.rndi(YMAX), c)
iteration++
report(testtime, iteration)
PUB demo_shadebobs(testtime) | iteration, x, y, xs, ys, p, offset, div, modifier
' Draws shadebobs along a sine-wave
ser.printf("demo_shadebobs - ")
div := 1024
offset := YMAX/2
_timer_set := testtime
iteration := 0
repeat while _timer_set
repeat x from 0 to XMAX
modifier := (abs(getct()) / 1_000_000)
y := offset + math.sin(x * modifier) / div
repeat ys from y to y+10
repeat xs from x to x+10
p := vga.point(xs, ys) + 1
if (p > vga.MAX_COLOR)
p := 0
vga.plot(xs, ys, p)
iteration++
report(testtime, iteration)
PUB demo_sinewave(testtime) | iteration, x, y, modifier, offset, div
' Draws a sine wave the length of the screen, influenced by the system counter
ser.printf("demo_sinewave - ")
div := 1024
offset := YMAX/2 ' Offset for Y axis
_timer_set := testtime
iteration := 0
repeat while _timer_set
repeat x from 0 to XMAX
modifier := (abs(getct()) / 1_000_000) ' Use system counter as modifier
y := offset + math.sin(x * modifier) / div
vga.plot(x, y, vga.MAX_COLOR)
vga.wait_vsync()
iteration++
vga.clear()
report(testtime, iteration)
PUB demo_seq_text(testtime) | iteration, col, row, ch, fg, bg
' Sequentially draws the whole font table to the screen
ch := $20
ser.printf("demo_seq_text - ")
_timer_set := testtime
iteration := 0
repeat while _timer_set
ch++
if (ch > $7F)
ch := $20
fg++
if (fg > vga.MAX_COLOR)
fg := 0
bg--
if (bg < 0)
bg := vga.MAX_COLOR
vga.fgcolor(fg)
vga.bgcolor(bg)
vga.char(ch)
iteration++
report(testtime, iteration)
PUB demo_rnd_text(testtime) | iteration, col, row, fg, bg
' Draws random characters to the screen
ser.printf("demo_rnd_text - ")
_timer_set := testtime
iteration := 0
repeat while _timer_set
fg++
if (fg > vga.MAX_COLOR)
fg := 0
bg--
if (bg < 0)
bg := vga.MAX_COLOR
vga.fgcolor(fg)
vga.bgcolor(bg)
vga.char(32 #> math.rndi(127))
iteration++
report(testtime, iteration)
PUB demo_tri_wave(testtime) | iteration, x, y, ydir
' Draws a simple triangular wave
ydir := 1
y := 0
ser.printf("demo_tri_wave - ")
_timer_set := testtime
iteration := 0
repeat while _timer_set
repeat x from 0 to XMAX
if (y == YMAX)
ydir := -1
if (y == 0)
ydir := 1
y := y + ydir
vga.plot(x, y, vga.MAX_COLOR)
vga.wait_vsync()
vga.clear()
iteration++
report(testtime, iteration)
PUB demo_wander(testtime) | iteration, x, y, d, c, movestep
' Draws randomly wandering pixels
x := XMAX/2
y := YMAX/2
movestep := 2
ser.printf("demo_wander - ")
_timer_set := testtime
iteration := 0
repeat while _timer_set
case d := math.rndi(4)
1:
x += movestep
if x > XMAX
x := 0
2:
x -= movestep
if x < 0
x := XMAX
3:
y += movestep
if y > YMAX
y := 0
4:
y -= movestep
if y < 0
y := YMAX
c := math.rndi(vga.MAX_COLOR)
vga.plot(x, y, c)
iteration++
report(testtime, iteration)
PUB report(testtime, iterations) | msec
' Display performance results
msec := (testtime * 1_000) / iterations
ser.printf( @"Total iterations: %d, iterations/sec: %d, ms/iteration: %d.%03.3d\n\r", ...
iterations, ...
iterations / (testtime/1000), ...
(msec/1000), ...
(msec//1000) )
PUB cog_timer() | time_left
repeat
repeat until _timer_set
time_left := _timer_set
repeat
time_left--
waitms(1)
while (time_left > 0)
_timer_set := 0
PUB setup()
ser.start()
ser.clear()
ser.printf("Serial terminal started - p2 @ %dMHz\n\r", clkfreq/1000000)
vga.defaults()
vga.start()
ser.strln(@"VGA 8bpp driver started")
vga.set_font(fnt.ptr(), fnt.setup())
vga.char_attrs(vga.TERMINAL) ' don't print glyphs for codes like 'CR' and 'LF'
vga.clear()
_timer_cog := cogspin(NEWCOG, cog_timer(), @_stack_timer)
CON
WIDTH = vga.WIDTH
HEIGHT = vga.HEIGHT
XMAX = WIDTH-1
YMAX = HEIGHT-1
BPL = WIDTH
DAT
{
Copyright 2024 Jesse Burt
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
}