-
Notifications
You must be signed in to change notification settings - Fork 12
/
DisplayExtensions.swift
611 lines (557 loc) · 19.8 KB
/
DisplayExtensions.swift
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
//
// DisplayExtensions.swift
// my41
//
// Created by Miroslav Perovic on 2/14/15.
// Copyright (c) 2015 iPera. All rights reserved.
//
import Foundation
import CoreGraphics
typealias Digits12 = [Digit]
typealias DisplaySegmentMap = UInt32
let emptyDigit12:[Digit] = [Digit](repeating: 0, count: 12)
let annunciatorStrings: [String] = ["BAT ", "USER ", "G", "RAD ", "SHIFT ", "0", "1", "2", "3", "4 ", "PRGM ", "ALPHA"]
let CTULookupRsrcName = "display"
let CTULookupRsrcType = "lookup"
var CTULookup: String? // lookup table hardware character index -> unichar
var CTULookupLength: Int? // actual lookup table length (file size)
let punctSegmentTable: [DisplaySegmentMap] = [
0x00000, // no punctuation
0x08000, // .
0x0C000, // :
0x18000, // ,
0x1C000 // ; (only used during startup segment test)
]
struct DisplayRegisters {
var A: Digits12 = emptyDigit12
var B: Digits12 = emptyDigit12
var C: Digits12 = emptyDigit12
var E: Bits12 = 0
}
extension Display {
enum DisplayShiftDirection {
case left
case right
}
enum DisplayTransitionSize {
case short
case long
}
enum DisplayRegisterSet : Int {
case ra = 1
case rb = 2
case rc = 4
case rab = 3
case rabc = 7
}
//MARK: - Registers manipulation
func readFromRegister(_ param: Bits4) {
// Implement READ f or READ DATA instruction with display as selected peripheral.
switch param {
case 0x0: //FLLDA
displayFetch(®isters, withDirection: .left, andSize: .long, withRegister: .ra, andData: &cpu.reg.C)
case 0x1: // FLLDB
displayFetch(®isters, withDirection: .left, andSize: .long, withRegister: .rb, andData: &cpu.reg.C)
case 0x2: // FLLDC
displayFetch(®isters, withDirection: .left, andSize: .long, withRegister: .rc, andData: &cpu.reg.C)
case 0x3: // FLLDAB
displayFetch(®isters, withDirection: .left, andSize: .long, withRegister: .rab, andData: &cpu.reg.C)
case 0x4: // FLLABC
displayFetch(®isters, withDirection: .left, andSize: .long, withRegister: .rabc, andData: &cpu.reg.C)
case 0x5: // READDEN
bitsToDigits(bits: Int(registers.E), destination: &cpu.reg.C, start: 0, count: 4)
return // doesn't change display
case 0x6: // FLSDC
displayFetch(®isters, withDirection: .left, andSize: .short, withRegister: .ra, andData: &cpu.reg.C)
case 0x7: // FRSDA
displayFetch(®isters, withDirection: .right, andSize: .short, withRegister: .ra, andData: &cpu.reg.C)
case 0x8: // FRSDB
displayFetch(®isters, withDirection: .right, andSize: .short, withRegister: .rb, andData: &cpu.reg.C)
case 0x9: // FRSDC
displayFetch(®isters, withDirection: .right, andSize: .short, withRegister: .rc, andData: &cpu.reg.C)
case 0xA: // FLSDA
displayFetch(®isters, withDirection: .left, andSize: .short, withRegister: .ra, andData: &cpu.reg.C) // Original: .RB
case 0xB: // FLSDB
displayFetch(®isters, withDirection: .left, andSize: .short, withRegister: .rb, andData: &cpu.reg.C)
case 0xC: // FRSDAB
displayFetch(®isters, withDirection: .right, andSize: .short, withRegister: .rab, andData: &cpu.reg.C)
case 0xD: // FLSDAB
displayFetch(®isters, withDirection: .left, andSize: .short, withRegister: .rab, andData: &cpu.reg.C)
case 0xE: // FRSABC
displayFetch(®isters, withDirection: .right, andSize: .short, withRegister: .rabc, andData: &cpu.reg.C)
case 0xF: // FLSABC
displayFetch(®isters, withDirection: .left, andSize: .short, withRegister: .rabc, andData: &cpu.reg.C)
default:
bus.abortInstruction("Unimplemented display operation")
}
scheduleUpdate()
}
func writeToRegister(_ param: Bits4) {
// Implement WRITE f instruction with display as selected peripheral.
switch param {
case 0x0: // SRLDA
shift(®isters, withDirection: .right, andSize: .long, withRegister: .ra, andData: &cpu.reg.C)
case 0x1: // SRLDB
shift(®isters, withDirection: .right, andSize: .long, withRegister: .rb, andData: &cpu.reg.C)
case 0x2: // SRLDC
shift(®isters, withDirection: .right, andSize: .long, withRegister: .rc, andData: &cpu.reg.C)
case 0x3: // SRLDAB
shift(®isters, withDirection: .right, andSize: .long, withRegister: .rab, andData: &cpu.reg.C)
case 0x4: // SRLABC
shift(®isters, withDirection: .right, andSize: .long, withRegister: .rabc, andData: &cpu.reg.C)
case 0x5: // SLLDAB
shift(®isters, withDirection: .left, andSize: .short, withRegister: .rab, andData: &cpu.reg.C)
case 0x6: // SLLABC
shift(®isters, withDirection: .left, andSize: .long, withRegister: .rabc, andData: &cpu.reg.C)
case 0x7: // SRSDA
shift(®isters, withDirection: .right, andSize: .short, withRegister: .ra, andData: &cpu.reg.C)
case 0x8: // SRSDB
shift(®isters, withDirection: .right, andSize: .short, withRegister: .rb, andData: &cpu.reg.C)
case 0x9: // SRSDC
shift(®isters, withDirection: .right, andSize: .short, withRegister: .rc, andData: &cpu.reg.C)
case 0xA: // SLSDA
shift(®isters, withDirection: .left, andSize: .short, withRegister: .ra, andData: &cpu.reg.C)
case 0xB: // SLSDB
shift(®isters, withDirection: .left, andSize: .short, withRegister: .rb, andData: &cpu.reg.C)
case 0xC: // SRSDAB
shift(®isters, withDirection: .right, andSize: .short, withRegister: .rab, andData: &cpu.reg.C)
case 0xD: // SLSDAB
shift(®isters, withDirection: .left, andSize: .short, withRegister: .rab, andData: &cpu.reg.C)
case 0xE: // SRSABC
shift(®isters, withDirection: .right, andSize: .short, withRegister: .rabc, andData: &cpu.reg.C)
case 0xF: // SLSABC
shift(®isters, withDirection: .left, andSize: .short, withRegister: .rabc, andData: &cpu.reg.C)
default:
bus.abortInstruction("Unimplemented display operation")
}
scheduleUpdate()
}
func displayWrite()
{
switch cpu.opcode.row {
case 0x0:
// 028 SRLDA WRA12L SRLDA
registers.A[0] = cpu.reg.C[0]
registers.A[1] = cpu.reg.C[1]
registers.A[2] = cpu.reg.C[2]
registers.A[3] = cpu.reg.C[3]
registers.A[4] = cpu.reg.C[4]
registers.A[5] = cpu.reg.C[5]
registers.A[6] = cpu.reg.C[6]
registers.A[7] = cpu.reg.C[7]
registers.A[8] = cpu.reg.C[8]
registers.A[9] = cpu.reg.C[9]
registers.A[10] = cpu.reg.C[10]
registers.A[11] = cpu.reg.C[11]
case 0x1:
// 068 SRLDB WRB12L SRLDB
registers.B[0] = cpu.reg.C[0]
registers.B[1] = cpu.reg.C[1]
registers.B[2] = cpu.reg.C[2]
registers.B[3] = cpu.reg.C[3]
registers.B[4] = cpu.reg.C[4]
registers.B[5] = cpu.reg.C[5]
registers.B[6] = cpu.reg.C[6]
registers.B[7] = cpu.reg.C[7]
registers.B[8] = cpu.reg.C[8]
registers.B[9] = cpu.reg.C[9]
registers.B[10] = cpu.reg.C[10]
registers.B[11] = cpu.reg.C[11]
case 0x2:
// 0A8 SRLDC WRC12L SRLDC
registers.C[0] = cpu.reg.C[0]
registers.C[1] = cpu.reg.C[1]
registers.C[2] = cpu.reg.C[2]
registers.C[3] = cpu.reg.C[3]
registers.C[4] = cpu.reg.C[4]
registers.C[5] = cpu.reg.C[5]
registers.C[6] = cpu.reg.C[6]
registers.C[7] = cpu.reg.C[7]
registers.C[8] = cpu.reg.C[8]
registers.C[9] = cpu.reg.C[9]
registers.C[10] = cpu.reg.C[10]
registers.C[11] = cpu.reg.C[11]
case 0x3:
// 0E8 SRLDAB WRAB6L SRLDAB
rotateDisplayRegisterLeft(®isters.A, times: 6)
rotateDisplayRegisterLeft(®isters.B, times: 6)
registers.A[6] = cpu.reg.C[0]
registers.B[6] = cpu.reg.C[1]
registers.A[7] = cpu.reg.C[2]
registers.B[7] = cpu.reg.C[3]
registers.A[8] = cpu.reg.C[4]
registers.B[8] = cpu.reg.C[5]
registers.A[9] = cpu.reg.C[6]
registers.B[9] = cpu.reg.C[7]
registers.A[10] = cpu.reg.C[8]
registers.B[10] = cpu.reg.C[9]
registers.A[11] = cpu.reg.C[10]
registers.B[11] = cpu.reg.C[11]
case 0x4:
// 128 SRLABC WRABC4L SRLABC ;also HP:SRLDABC
rotateDisplayRegisterLeft(®isters.A, times: 4)
rotateDisplayRegisterLeft(®isters.B, times: 4)
rotateDisplayRegisterLeft(®isters.C, times: 4)
registers.A[8] = cpu.reg.C[0]
registers.B[8] = cpu.reg.C[1]
registers.C[8] = cpu.reg.C[2] & 0x01
registers.A[9] = cpu.reg.C[3]
registers.B[9] = cpu.reg.C[4]
registers.C[9] = cpu.reg.C[5] & 0x01
registers.A[10] = cpu.reg.C[6]
registers.B[10] = cpu.reg.C[7]
registers.C[10] = cpu.reg.C[8] & 0x01
registers.A[11] = cpu.reg.C[9]
registers.B[11] = cpu.reg.C[10]
registers.C[11] = cpu.reg.C[11] & 0x01
case 0x5:
// 168 SLLDAB WRAB6R SLLDAB
rotateDisplayRegisterRight(®isters.A, times: 6)
rotateDisplayRegisterRight(®isters.B, times: 6)
registers.A[5] = cpu.reg.C[0]
registers.B[5] = cpu.reg.C[1]
registers.A[4] = cpu.reg.C[2]
registers.B[4] = cpu.reg.C[3]
registers.A[3] = cpu.reg.C[4]
registers.B[3] = cpu.reg.C[5]
registers.A[2] = cpu.reg.C[6]
registers.B[1] = cpu.reg.C[7]
registers.A[1] = cpu.reg.C[8]
registers.B[1] = cpu.reg.C[9]
registers.A[0] = cpu.reg.C[10]
registers.B[0] = cpu.reg.C[11]
case 0x6:
// 1A8 SLLABC WRABC4R SLLABC ;also HP:SLLDABC
rotateDisplayRegisterRight(®isters.A, times: 4)
rotateDisplayRegisterRight(®isters.B, times: 4)
rotateDisplayRegisterRight(®isters.C, times: 4)
registers.A[3] = cpu.reg.C[0]
registers.B[3] = cpu.reg.C[1]
registers.C[3] = cpu.reg.C[2] & 0x01
registers.A[2] = cpu.reg.C[3]
registers.B[2] = cpu.reg.C[4]
registers.C[2] = cpu.reg.C[5] & 0x01
registers.A[1] = cpu.reg.C[6]
registers.B[1] = cpu.reg.C[7]
registers.C[1] = cpu.reg.C[8] & 0x01
registers.A[0] = cpu.reg.C[9]
registers.B[0] = cpu.reg.C[10]
registers.C[0] = cpu.reg.C[11] & 0x01
case 0x7:
// 1E8 SRSDA WRA1L SRSDA
rotateDisplayRegisterLeft(®isters.A, times: 1)
registers.A[11] = cpu.reg.C[0]
case 0x8:
// 228 SRSDB WRB1L SRSDB
rotateDisplayRegisterLeft(®isters.B, times: 1)
registers.B[11] = cpu.reg.C[0]
case 0x9:
// 268 SRSDC WRC1L SRSDC
rotateDisplayRegisterLeft(®isters.C, times: 1)
registers.C[11] = cpu.reg.C[0] & 0x01
case 0xa:
// 2A8 SLSDA WRA1R SLSDA
rotateDisplayRegisterRight(®isters.A, times: 1)
registers.A[0] = cpu.reg.C[0]
case 0xb:
// 2E8 SLSDB WRB1R SLSDB
rotateDisplayRegisterRight(®isters.B, times: 1)
registers.B[0] = cpu.reg.C[0]
case 0xc:
// 328 SRSDAB WRAB1L SRSDAB ;Zenrom manual incorrectly says this is WRC1R
rotateDisplayRegisterLeft(®isters.A, times: 1)
rotateDisplayRegisterLeft(®isters.B, times: 1)
registers.A[11] = cpu.reg.C[0]
registers.B[11] = cpu.reg.C[1]
case 0xd:
// 368 SLSDAB WRAB1R SLSDAB
rotateDisplayRegisterRight(®isters.A, times: 1)
rotateDisplayRegisterRight(®isters.B, times: 1)
registers.A[0] = cpu.reg.C[0]
registers.B[0] = cpu.reg.C[1]
case 0xe:
// 3A8 SRSABC WRABC1L SRSABC ;also HP:SRSDABC
rotateDisplayRegisterLeft(®isters.A, times: 1)
rotateDisplayRegisterLeft(®isters.B, times: 1)
rotateDisplayRegisterLeft(®isters.C, times: 1)
registers.A[11] = cpu.reg.C[0]
registers.B[11] = cpu.reg.C[1]
registers.C[11] = cpu.reg.C[2] & 0x01
case 0xf:
// 3E8 SLSABC WRABC1R SLSABC ;also HP:SLSDABC
rotateDisplayRegisterRight(®isters.A, times: 1)
rotateDisplayRegisterRight(®isters.B, times: 1)
rotateDisplayRegisterRight(®isters.C, times: 1)
registers.A[0] = cpu.reg.C[0]
registers.B[0] = cpu.reg.C[1]
registers.C[0] = cpu.reg.C[2] & 0x01
default:
break
}
scheduleUpdate()
}
func displayFetch(
_ registers: inout DisplayRegisters,
withDirection direction:DisplayShiftDirection,
andSize size:DisplayTransitionSize,
withRegister regset:DisplayRegisterSet,
andData data: inout Digits14
)
{
/*
Fetch digits from the specified registers, rotating them in the specified direction,
and assemble them into the specified destination.
For size == LONG, fetches a total of 12 digits into the destination;
for size == SHORT, fetches one digit from each specified register.
*/
var cp = 0
while cp < 12 {
if (regset.rawValue & DisplayRegisterSet.ra.rawValue) != 0 {
fetchDigit(direction, from: ®isters.A, to: &data[cp])
cp += 1
}
if (regset.rawValue & DisplayRegisterSet.rb.rawValue) != 0 {
fetchDigit(direction, from: ®isters.B, to: &data[cp])
cp += 1
}
if (regset.rawValue & DisplayRegisterSet.rc.rawValue) != 0 {
fetchDigit(direction, from: ®isters.C, to: &data[cp])
cp += 1
}
if size == .short {
break
}
}
}
@discardableResult
func fetchDigit(
_ direction: DisplayShiftDirection,
from register: inout Digits12,
to dst: inout Digit
) -> Digits12
{
/*
Fetch a digit from the appropriate end of the given register into the specified destination,
and rotate the register in the specified direction.
*/
switch direction {
case .left:
dst = register[11]
rotateRegisterLeft(®ister)
case .right:
dst = register[0]
rotateRegisterRight(®ister)
}
return register
}
func rotateRegisterLeft(_ register: inout Digits12)
{
let temp = register[11]
for idx in Array((1...11).reversed()) {
register[idx] = register[idx - 1]
}
register[0] = temp
}
func rotateDisplayRegisterLeft(_ register: inout Digits12, times: Int) {
if times > 0 {
for _ in 0..<times {
let temp = register[0]
for idx in 0...10 {
register[idx] = register[idx + 1]
}
register[11] = temp
}
}
}
func rotateRegisterRight(_ register: inout Digits12) {
let temp = register[0]
for idx in 0...10 {
register[idx] = register[idx + 1]
}
register[11] = temp
}
func rotateDisplayRegisterRight(_ register: inout Digits12, times: Int)
{
if times > 0 {
for _ in 0..<times {
let temp = register[11]
for idx in Array((1...11).reversed()) {
register[idx] = register[idx - 1]
}
register[0] = temp
}
}
}
//MARK: -
func shift(
_ registers: inout DisplayRegisters,
withDirection direction:DisplayShiftDirection,
andSize size:DisplayTransitionSize,
withRegister regset:DisplayRegisterSet,
andData data: inout Digits14
)
{
/*
Distribute digits from the given source and rotate them into the specified registers.
For size == LONG, shifts a total of 12 digits from the source;
for size == SHORT, shifts one digit into each specified register.
*/
var cp = 0
while cp < 12 {
if (regset.rawValue & DisplayRegisterSet.ra.rawValue) != 0 {
shiftDigit(direction, from: ®isters.A, withFilter: data[cp])
cp += 1
}
if (regset.rawValue & DisplayRegisterSet.rb.rawValue) != 0 {
shiftDigit(direction, from: ®isters.B, withFilter: data[cp])
cp += 1
}
if (regset.rawValue & DisplayRegisterSet.rc.rawValue) != 0 {
shiftDigit(direction, from: ®isters.C, withFilter: data[cp])
cp += 1
}
if size == .short {
break
}
}
}
func shiftDigit(_ direction: DisplayShiftDirection, from register: inout Digits12, withFilter src: Digit) {
// Rotate the given digit into the specified register in the specified direction.
switch direction {
case .left:
rotateRegisterLeft(®ister)
register[0] = src
case .right:
rotateRegisterRight(®ister)
register[11] = src
}
}
func segmentsForCell(_ i: Int) -> DisplaySegmentMap {
/*
Determine which segments should be on for cell i based on the contents of the display registers.
Note that cells are numbered from left to right, which is the opposite of the digit numbering in the display registers.
*/
let j = 11 - i
let nineBitCode: Int = Int((Int(registers.C[j]) << 8) | (Int(registers.B[j]) << 4) | (Int(registers.A[j]))) & 0x1ff
let charCode: Int = ((nineBitCode & 0x100) >> 2) | (nineBitCode & 0x3f)
let punctCode = Int((Int(registers.C[j]) & 0x2) << 1) | (nineBitCode & 0xc0) >> 6
return displayFont[Int(charCode)] | punctSegmentTable[Int(punctCode)]
}
func annunciatorOn(_ i: Int) -> Bool {
/*
Determine whether annunciator number i should be on based on the contents of the E register.
Note that i is not the same as the bit number in E, because the annunciators are numbered from left to right.
*/
let j: Bits12 = 11 - Bits12(i)
return (registers.E & (1 << j)) != 0
}
func scheduleUpdate() {
updateCountdown = 2
}
func displayToggle() {
// Toggle the display between on and off.
on = !on
scheduleUpdate()
}
func displayOff() {
if on {
on = false
scheduleUpdate()
}
}
func cellWidth() -> CGFloat {
return self.bounds.size.width / CGFloat(numDisplayCells)
}
// Read the current content of the display as an String:
// Reads the actual hardware registers and converts the content to a normalized
// unicode string (suppressing leading and trailing spaces).
func readDisplayAsText() -> String {
// access the hardware display registers A, B, C:
let r = registers
// we need up to two characters per display cell (character+punctuation):
var text: String = ""
// prepare the punctuation lookup table (8 characters for 3 bit punctuation code)
let punct: String = " .:,;???"
// loop through the display cells and translate their content:
for idx in Array((0...numDisplayCells-1).reversed()) {
// assemble the actual hardware character index from the register bits:
// charCode = C0 B1 B0 A3 A2 A1 A0
let charCode = ((r.C[idx] & 0x1) << 6) | ((r.B[idx] & 0x3) << 4) | (r.A[idx] & 0xf)
// if valid, look up unicode for X-41 hardware character index:
if charCode < UInt8(CTULookupLength!) {
// translate:
let aChar: Character = CTULookup![Int(charCode)]
let scalars = String(aChar).unicodeScalars
// only if we already have some valid characters or if this one is valid:
if scalars[scalars.startIndex].value != 0x20 {
// not a leading space: save
text.append(aChar)
}
}
let punctCode = ((r.C[idx] & 0x2) << 1) | ((r.B[idx] & 0xc) >> 2)
// if there is any punctuation, insert the respective character from the table:
if punctCode != 0 {
text.append(punct[Int(punctCode)])
}
}
// now return the completed string as an NSString without any trailing spaces:
return text
}
func timeSlice(_ timer: Foundation.Timer) {
if (updateCountdown > 0) {
updateCountdown -= 1
if (updateCountdown == 0) {
setNeedsDisplay(self.bounds)
}
}
}
//MARK: - Halfnut
func halfnutWrite()
{
// REG=C 5
if cpu.opcode.row == 5 {
contrast = cpu.reg.C[0]
}
}
func halfnutRead() {
if cpu.opcode.row == 5 {
cpu.reg.C[0] = contrast
}
}
func digits12ToString(_ register: Digits12) -> String {
var result = String()
for idx in Array((0...11).reversed()) {
result += NSString(format:"%1X", register[idx]) as String
}
return result
}
//MARK: - Font support
func loadFont(_ resourceName: String) -> DisplayFont {
var font: DisplayFont = DisplayFont(repeating: 0, count: 128)
let filename = Bundle.main.path(forResource: resourceName, ofType: "hpfont")
do {
// Read the data from the file
let data = try Data(contentsOf: URL(fileURLWithPath: filename!), options: [.mappedIfSafe])
// Calculate the number of UInt32 values in the data
let count = data.count / MemoryLayout<UInt32>.size
// Extract UInt32 values from the data with big-endian byte order
for i in 0..<count {
let offset = i * MemoryLayout<DisplaySegmentMap>.size
let value = data[offset..<offset+MemoryLayout<UInt32>.size].withUnsafeBytes { $0.load(as: UInt32.self) }
font[i] = value.bigEndian
}
return font
} catch _ {
fatalError()
}
}
//MARK: - Peripheral Protocol Method
func pluggedIntoBus(_ theBus: Bus?) {
// self.aBus = theBus
}
func writeDataFrom(_ data: Digits14) {
// Implement WRITE DATA instruction with display as selected peripheral.
registers.E = digitsToBits(digits: data, nbits: 12)
scheduleUpdate()
}
}