-
Notifications
You must be signed in to change notification settings - Fork 1
/
display.vga.bitmap-8bpp.spin2
369 lines (290 loc) · 11.1 KB
/
display.vga.bitmap-8bpp.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
{
----------------------------------------------------------------------------------------------------
Filename: display.vga.bitmap-8bpp.spin2
Description: 8-bits per pixel bitmap VGA driver
* QVGA (320x240 - signalled as 640x480)
Author: Jesse Burt
Started: Apr 6, 2020
Updated: Sep 6, 2024
Copyright (c) 2024 - See end of file for terms of use.
----------------------------------------------------------------------------------------------------
NOTE: This is based on raycast5b_SpinDisplay2a.spin2,
originally written by Rayman/Ray Allen
}
#define MEMMV_NATIVE bytemove
#include "graphics.common.spin2h"
#ifdef GFX_DIRECT
#error "direct drawing to display not supported"
#endif
CON
{ default I/O settings - these can be overridden in the parent object }
BASEPIN = 0
INTENSITY = 127 ' 0..127
WIDTH = 320 ' only 320 supported
HEIGHT = 240 ' only 240 supported
{ default timings }
VF = 10
VB = 33
HF = 16
HS = 96
HB = 48
HV = 640
XMAX = WIDTH-1
YMAX = HEIGHT-1
CENTERX = WIDTH/2
CENTERY = HEIGHT/2
PIX_CLK = 25_175_000
BYTESPERPX = 1
MAX_COLOR = 255
HSYNC_PAR = 0
RED_PAR = 1
GREEN_PAR = 2
BLUE_PAR = 3
VSYNC_PAR = 4
PTR_FB_PAR = 5
OBJ
streamer: "core.con.streamer"
VAR
long _palette[256]
long _cog, _params[6]
long _ptr_dispbuff
byte _framebuffer[(WIDTH*HEIGHT)]
byte _vsync_pin
PUB start(): status
' Start using default I/O settings
return startx(BASEPIN, WIDTH, HEIGHT, @_framebuffer)
PUB startx(BASE_PIN, DISP_W, DISP_H, ptr_dispbuff): status
' Start using custom I/O settings
' BASE_PIN: lowest pin number of 5-pin (consecutive) block
' DISP_W, DISP_H: display dimensions (currently only 320, 240 is supported)
' ptr_dispbuff: pointer to display buffer
_ptr_palette := @_palette
_params[HSYNC_PAR] := BASEPIN
_params[RED_PAR] := BASEPIN+1
_params[GREEN_PAR] := BASEPIN+2
_params[BLUE_PAR] := BASEPIN+3
_params[VSYNC_PAR] := BASEPIN+4
_params[PTR_FB_PAR] := ptr_dispbuff
set_dims(DISP_W, DISP_H)
set_address(ptr_dispbuff)
_ptr_dispbuff := ptr_dispbuff
_cog := status := coginit(NEWCOG, @DisplayEntry, @_params) + 1
return
PUB stop()
' Stop the driver
if ( _cog )
cogstop(_cog-1)
PUB defaults()
' Default timings for 640x480 signalling
set_timings(VF, VB, HF, HS, HB, HV)
default_palette()
PUB default_palette() | i, r, g, b, c
' Set up palette
r := 0
g := 0
b := 0
_palette[0] := 0
repeat i from 0 to MAX_COLOR
case i
1..63:
r += 4
g := 0
b := 0
64..127:
r := 0
g += 4
b := 0
128..191:
r := 0
g := 0
b += 4
192..254:
r += 4
g += 4
b := 0
255:
r := 255
g := 255
b := 255
c := 0 | (r << 16) | (g << 8) | b
_palette[i] := c
PUB clear()
' Clear the display
bytefill(_ptr_drawbuffer, _bgcolor, _buff_sz)
PUB draw_to(addr)
' Set address of (optional) draw/render buffer
' NOTE: This is typically used as an offscreen buffer,
' to subsequently be copied to the display or "live" buffer,
' once a complete frame is rendered.
_ptr_drawbuffer := addr
PUB palette_ptr(): addr
' Pointer to palette data
return @_palette
PUB plot(x, y, color)
' Plot pixel at (x, y) in color
if ( (x < 0) or (x > _disp_xmax) or (y < 0) or (y > _disp_ymax) )
return ' coords out of bounds, ignore
byte[_ptr_drawbuffer][x + (y * _disp_width)] := color
PUB point(x, y): pix_clr
' Get color of pixel at x, y
x := 0 #> x <# _disp_xmax
y := 0 #> y <# _disp_ymax
return byte[_ptr_drawbuffer][x + (y * _disp_width)]
PUB set_palette(p_pal) | src, dest, r, g, b
' Set up the driver with a custom palette
' p_pal: pointer to custom palette (768 bytes, 256 entries of RGB888 words each)
dest := 0
repeat 256
r := byte[p_pal++]
g := byte[p_pal++]
b := byte[p_pal++]
_palette[dest++] := 0 | (r << 16) | (g << 8) | b
PUB set_timings(vfront, vback, hfront, hsync, hback, hvisible)
' Set video timings
' vfront: Vertical front porch
' vback: Vertical back porch
' hfront: Horizontal front porch
' hsync: Horizontal sync pulse
' hback: Horizontal back porch
' hvisible: Horizontal visible lines
_vf := vfront
_vb := vback
_hf := STREAMER_SETUP | (1 << streamer.LUTBASE) + hfront/2
_hs := STREAMER_SETUP | (1 << streamer.LUTBASE) + hsync/2
_hb := STREAMER_SETUP | (1 << streamer.LUTBASE) + hback/2
_hv := STREAMER_SETUP | (1 << streamer.LUTBASE) + hvisible/2
PUB show()
' Write the draw buffer to the display
' NOTE: This is only required when using double-buffering
longmove(_ptr_dispbuff, _ptr_drawbuffer, _buff_sz/4)
PUB vsync(): state
' Get current state of vsync pin
return _pinr(_params[VSYNC_PAR])
PUB wait_vsync()
' Wait for vertical sync signal to be high
repeat until _pinr(_params[VSYNC_PAR]) == 0
repeat until _pinr(_params[VSYNC_PAR]) == 1
PRI memfill(xs, ys, val, count)
' Fill region of display buffer memory
' xs, ys: Start of region
' val: Color
' count: Number of consecutive memory locations to write
bytefill(_ptr_drawbuffer + (xs + (ys * _bytesperln)), val, count)
DAT
orgh
DisplayEntry
org 0
'
'
' Setup
'
'l c i o e c
' calculate streamer frequency
rdlong pa, #@clkfreq
qfrac ##PIX_CLK, pa
getqx pa
shr pa, #2
setxfrq pa
rdfast #0, _ptr_palette
rep @.end, #$100
rflong y
shl y, #8
wrlut y, x
add x, #1
.end
rdlong hsync_pin, ptra++
rdlong red_pin, ptra++
rdlong green_pin, ptra++
rdlong blue_pin, ptra++
rdlong vsync_pin, ptra++
rdlong framebuff_addr, ptra++
'the next 4 lines may be commented out to bypass level scaling
setcy ##INTENSITY << 24 'r set colorspace for rgb
setci ##INTENSITY << 16 'g
setcq ##INTENSITY << 08 'b
setcmod #%01_0_000_0 'enable colorspace conversion
'RJA dacmodes changed for real P2
cogid mycogid
shl mycogid, #8
or dacmode_s, mycogid
or dacmode_c, mycogid
'RJA dacmodes changed for real P2
wrpin dacmode_s, hsync_pin 'enable dac modes in pins 0..3
wrpin dacmode_c, red_pin
wrpin dacmode_c, green_pin
wrpin dacmode_c, blue_pin
dirh hsync_pin
dirh red_pin
dirh green_pin
dirh blue_pin
'
' Field loop
'
hfield mov x, _vb 'top blanks
call #blank
mov m_buf, framebuff_addr
mov x, #480/2 'set visible lines
lineloop call #hsync 'do horizontal sync
rdfast ##640*1/64/2, m_buf ' D=64 byte units (5 here, so 5*64 = 320)
xcont m_rf, #0 'visible line
call #hsync 'do horizontal sync
xcont m_rf, #0 'visible line
add m_buf, #320
djnz x, #lineloop 'another line?
mov x, _vf 'bottom blanks
call #blank
drvnot vsync_pin 'sync on
mov x, #2 'sync blanks
call #blank
drvnot vsync_pin 'sync off
jmp #hfield 'loop
'
' Subroutines
'
blank call #hsync 'blank lines
xcont _hv, #0
_ret_ djnz x, #blank
hsync xcont _hf, #0 'horizontal sync
xcont _hs, #1
_ret_ xcont _hb, #0
'
' Initialized data
'
dacmode_s long P_DAC_124R_3V | P_OE ' HSync
dacmode_c long P_DAC_75R_2V | P_OE ' R, G, B
_vf long 0
_vb long 0
_hf long 0 ' Before sync
_hs long 0 ' Sync
_hb long 0 ' Before visible
_hv long 0 ' Visible
m_rf long STREAMER_SETUP | (8 << streamer.LUTBASE) | (640/2)
framebuff_addr long 0
hsync_pin long 0
red_pin long 0
green_pin long 0
blue_pin long 0
vsync_pin long 0
x long 0
y long 0
m_buf long 0
_ptr_palette long 0
mycogid long 0
CON
STREAMER_SETUP = X_IMM_2X16_2DAC8 | X_DACS_3_2_1_0
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.
}