-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaa505x.go
More file actions
440 lines (400 loc) · 8.14 KB
/
Copy pathsaa505x.go
File metadata and controls
440 lines (400 loc) · 8.14 KB
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
package teletext
import "fmt"
type saa505x struct {
code uint8
heldChar uint8
nextCharType uint8
currCharType uint8
heldCharType uint8
charData uint16
bit int
crs int
ra int
bg, fg, pc, color uint8
graphics bool
separated bool
flash bool
boxed bool
doubleHeight bool
doubleHeightOld bool
doubleHeightTop bool
doubleHeightBot bool
holdChar bool
holdClear bool
holdOff bool
frameCount int
cols, rows, size int
rom []byte
}
func newSAA505x(rom []byte) *saa505x {
dev := &saa505x{rom: rom}
dev.reset()
return dev
}
func (dev *saa505x) reset() {
dev.ra = 0
dev.doubleHeight = false
dev.doubleHeightBot = false
}
func (dev *saa505x) process(b uint8) {
dev.doubleHeightOld = dev.doubleHeight
dev.pc = dev.fg
dev.currCharType = dev.nextCharType
if b < 0x20 {
dev.processControl(b)
if dev.holdChar && dev.doubleHeight == dev.doubleHeightOld {
b = dev.heldChar
if b >= 0x40 && b < 0x60 {
b = 0x20
}
dev.currCharType = dev.heldCharType
} else {
b = 0x20
}
} else if dev.graphics {
dev.heldChar = b
dev.heldCharType = dev.currCharType
}
ra := dev.ra
if dev.doubleHeightOld {
ra >>= 1
if dev.doubleHeightBot {
ra += 10
}
}
if dev.flash && dev.frameCount > 38 {
b = 0x20
}
if dev.doubleHeightBot && !dev.doubleHeight {
b = 0x20
}
if dev.holdOff {
dev.holdChar = false
dev.heldChar = 0x20
}
if dev.holdClear {
dev.heldChar = 0x20
}
if dev.currCharType == typeAlphanumeric || !bitSet(b, 5) {
rb := ra
if ra&1 == 0 {
rb--
} else {
rb++
}
dev.charData = roundCharacter(dev.romData(b, ra), dev.romData(b, rb))
} else {
dev.charData = dev.gfxData(b, ra, dev.currCharType == typeSeparated)
}
}
func (dev *saa505x) processControl(b uint8) {
dev.holdClear = false
dev.holdOff = false
switch b {
case
controlAlphaRed,
controlAlphaGreen,
controlAlphaYellow,
controlAlphaBlue,
controlAlphaMagenta,
controlAlphaCyan,
controlAlphaWhite:
dev.graphics = false
dev.holdClear = true
dev.fg = b & 0x07
dev.setNextCharType()
case controlFlash:
dev.flash = true
case controlSteady:
dev.flash = true
case controlStartBox, controlEndBox:
// TODO
case controlNormalHeight, controlDoubleHeight:
dev.doubleHeight = (b & 1) != 0
if dev.doubleHeight {
dev.doubleHeightOld = true
}
case
controlMosaicRed,
controlMosaicGreen,
controlMosaicYellow,
controlMosaicBlue,
controlMosaicMagenta,
controlMosaicCyan,
controlMosaicWhite:
dev.graphics = true
dev.fg = b & 0x07
dev.setNextCharType()
case controlConcealDisplay:
dev.fg = dev.bg
dev.pc = dev.bg
case controlContiguousMosaic:
dev.separated = false
dev.setNextCharType()
case controlSeparatedMosaic:
dev.separated = true
dev.setNextCharType()
case controlBlackBackground:
dev.bg = 0
case controlNewBackground:
dev.bg = dev.fg
case controlHoldMosaic:
dev.holdChar = true
case controlReleaseMosaic:
dev.holdChar = false
}
}
func (dev *saa505x) setNextCharType() {
if dev.graphics {
if dev.separated {
dev.nextCharType = typeSeparated
} else {
dev.nextCharType = typeContiguous
}
} else {
dev.nextCharType = typeAlphanumeric
}
}
func (dev *saa505x) romData(b uint8, row int) (c uint16) {
if row < 0 || row >= 20 {
} else {
c = uint16(dev.rom[int(b*10)+(row>>1)])
c = ((c & 0x01) * 0x03) + ((c & 0x02) * 0x06) + ((c & 0x04) * 0x0c) + ((c & 0x08) * 0x18) + ((c & 0x10) * 0x30)
}
return
}
func (dev *saa505x) gfxData(b uint8, row int, separated bool) (c uint16) {
switch row >> 1 {
case 0, 1:
if b&0x01 != 0 {
c += 0xfc0 // bit 1 top left
}
if b&0x02 != 0 {
c += 0x03f // bit 2 top right
}
if separated {
c &= 0x3cf
}
case 2:
if separated {
break
}
if b&0x01 != 0 {
c += 0xfc0 // bit 1 top left
}
if b&0x02 != 0 {
c += 0x03f // bit 2 top right
}
case 3, 4, 5:
if b&0x04 != 0 {
c += 0xfc0 // bit 3 center left
}
if b&0x08 != 0 {
c += 0x03f // bit 4 center right
}
if separated {
c &= 0x3cf
}
case 6:
if separated {
break
}
if b&0x04 != 0 {
c += 0xfc0 // bit 3 center left
}
if b&0x08 != 0 {
c += 0x03f // bit 4 center right
}
case 7, 8:
if b&0x10 != 0 {
c += 0xfc0 // bit 3 bottom left
}
if b&0x40 != 0 {
c += 0x03f // bit 4 bottom right
}
if separated {
c &= 0x3cf
}
case 9:
if separated {
break
}
if b&0x10 != 0 {
c += 0xfc0 // bit 3 bottom left
}
if b&0x40 != 0 {
c += 0x03f // bit 4 bottom right
}
}
return
}
func (dev *saa505x) dataEntryWindow() {
dev.ra = 19
dev.frameCount++
if dev.frameCount > 50 {
dev.frameCount = 0
}
}
// load output shift register enable
func (dev *saa505x) lose() {
dev.ra = (dev.ra + 1) % 20
if dev.ra == 0 {
if dev.doubleHeightBot {
dev.doubleHeightBot = false
} else {
dev.doubleHeightBot = dev.doubleHeightOld
}
}
dev.fg = 7
dev.bg = 0
dev.graphics = false
dev.separated = false
dev.flash = false
dev.boxed = false
dev.holdChar = false
dev.heldChar = 0x20
dev.nextCharType = typeAlphanumeric
dev.heldCharType = typeAlphanumeric
dev.doubleHeight = false
dev.doubleHeightOld = false
dev.bit = 11
}
func (dev *saa505x) pixelClock() {
if bitSet16(dev.charData, uint8(dev.bit)) {
dev.color = dev.pc
} else {
dev.color = dev.bg
}
dev.bit--
if dev.bit < 0 {
dev.bit = 11
}
}
func bitSet(b, bit uint8) bool {
return (b>>bit)&1 != 0
}
func bitSet16(b uint16, bit uint8) bool {
return (b>>bit)&1 != 0
}
func roundCharacter(a, b uint16) uint16 {
return a | ((a >> 1) & b & ^(b >> 1)) | ((a << 1) & b & ^(b << 1))
}
const (
controlAlphaBlack = iota
controlAlphaRed
controlAlphaGreen
controlAlphaYellow
controlAlphaBlue
controlAlphaMagenta
controlAlphaCyan
controlAlphaWhite
controlFlash
controlSteady
controlEndBox
controlStartBox
controlNormalHeight
controlDoubleHeight
controlDoubleWidth
controlDoubleSize
controlMosaicBlack
controlMosaicRed
controlMosaicGreen
controlMosaicYellow
controlMosaicBlue
controlMosaicMagenta
controlMosaicCyan
controlMosaicWhite
controlConcealDisplay
controlContiguousMosaic
controlSeparatedMosaic
controlESC
controlBlackBackground
controlNewBackground
controlHoldMosaic
controlReleaseMosaic
)
func codeName(code uint8) string {
switch code {
case controlAlphaBlack:
return "AlphaBlack"
case controlAlphaRed:
return "AlphaRed"
case controlAlphaGreen:
return "AlphaGreen"
case controlAlphaYellow:
return "AlphaYellow"
case controlAlphaBlue:
return "AlphaBlue"
case controlAlphaMagenta:
return "AlphaMagenta"
case controlAlphaCyan:
return "AlphaCyan"
case controlAlphaWhite:
return "AlphaWhite"
case controlFlash:
return "Flash"
case controlSteady:
return "Steady"
case controlEndBox:
return "EndBox"
case controlStartBox:
return "StartBox"
case controlNormalHeight:
return "NormalHeight"
case controlDoubleHeight:
return "DoubleHeight"
case controlDoubleWidth:
return "DoubleWidth"
case controlDoubleSize:
return "DoubleSize"
case controlMosaicBlack:
return "MosaicBlack"
case controlMosaicRed:
return "MosaicRed"
case controlMosaicGreen:
return "MosaicGreen"
case controlMosaicYellow:
return "MosaicYellow"
case controlMosaicBlue:
return "MosaicBlue"
case controlMosaicMagenta:
return "MosaicMagenta"
case controlMosaicCyan:
return "MosaicCyan"
case controlMosaicWhite:
return "MosaicWhite"
case controlConcealDisplay:
return "ConcealDisplay"
case controlContiguousMosaic:
return "ContiguousMosaic"
case controlSeparatedMosaic:
return "SeparatedMosaic"
case controlESC:
return "ESC"
case controlBlackBackground:
return "BlackBackground"
case controlNewBackground:
return "NewBackground"
case controlHoldMosaic:
return "HoldMosaic"
case controlReleaseMosaic:
return "ReleaseMosaic"
default:
return fmt.Sprintf("%#02x", code)
}
}
const (
_ = iota
typeAlphanumeric
typeContiguous
typeSeparated
)
const (
charsetG0P = iota /* primary G0 */
charsetG0S /* secondary G0 */
charsetG1C /* G1 contiguous */
charsetG1S /* G1 separate */
charsetG2
charsetG3
charsetOffsetDRCS = 32
)