-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlcd.mac
More file actions
698 lines (577 loc) · 18.2 KB
/
lcd.mac
File metadata and controls
698 lines (577 loc) · 18.2 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
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
.comment\
*************************************************************************
* *
* Hitachi LCD Driver Module for Colossus Computer *
* *
* March 27, 1987 *
* *
************************************************************************* \
.z80 ; separately compiled module
BS equ 08h ; back space
LF equ 0ah ; line feed
CLEARSCRN equ 0ch ; clear display
CR equ 0dh ; carriage return
CURSDWN equ 01ah ; cursor down
CURSUP equ 017h ; cursor up
CURSRGHT equ 013h ; cursor right
CURSLEFT equ 001h ; cursor left
; ----- External Declarations ------
public lcd$table
extrn l1 ; lcd line 1 memory image (80 bytes)
extrn l2 ; lcd line 2 memory image (80 bytes)
extrn lcd$column ; last column (0-79) accessed
extrn lcd$line ; last lcd row accessed (0-1)
extrn lcd$cr ; current lcd control register
extrn lcd$dr ; current lcd data register
; The lcd may be split into two windows - the top line and the bottom line.
; When split=0, then the lcd is in full screen mode.
; When split=1, the top line is a scrolling single line window, and the
; bottom line is a non-scrolling window. Each is accessed
; by moving the cursor into that window.
extrn split ; lcd split(1)/full-screen(0) mode
; ----- Lcd Interface -----
lcd$1$ctrl equ 28h ; lcd1 left display control register
lcd$1$data equ 29h ; lcd1 left display data register
lcd$2$ctrl equ 2ah ; lcd1 right display control register
lcd$2$data equ 2bh ; lcd1 right display data register
; ---- General Lcd Equates -----
; The lcds consist of two 40 segments each consisting of two lines, each
; of which may be programmed separately. The next screen address to write
; into can be programmed by writing 'lcdta + top line offset' to position
; the top line write address, or 'lcdba + bottom line offset' to position
; to the bottom line write address.
lcdta equ 080h ; top lcd line address command offset
lcdba equ 0c0h ; bottom lcd line address command offset
; ----- Lcd commands -----
lcdrst equ 10h ;lcd reset (independent for both sides)
lcdclr equ 01h ;lcd clear
lcdch equ 02h ;lcd cursor home
lcdcr equ 03h ;lcd carriage return (no LF)
lcdcfd equ 04h ;set cursor direction forward
lcdcrv equ 05h ;set cursor direction reverse
lcdcin equ 06h ;set cursor increment
lcdcde equ 07h ;set cursor decrement
lcdcu equ 08h ;set underline cursor
lcdcbl equ 09h ;set blinking block cursor
lcdcub equ 0bh ;set underline cursor blinking (+ 08h f'n)
lcdcuc equ 0ah ;set underline cursor cont. (+ 08h f'n)
lcdcon equ 0fh ;set cursor on
lcdcof equ 0eh ;set cursor off
lcddon equ 0dh ;set display on
lcddof equ 0ch ;set display off
; ----- Lcd Related Equates -----
busy equ 80h ; lcd busy indicator
chcnt equ 80 ; characters / line
segwid equ 40 ; characters / lcd segment
;----------------------------------------------------------------------
; This is the LCD function dispatch table
lcd$table: defw lcd$print ; print A to LCD
defw lcd$on ; turn display on
defw lcd$off ; turn display off
defw lcd$on$cursor ; turn cursor on
defw lcd$off$cursor ; turn cursor off
defw lcd$move$cursor ; move cursor to D,E
defw lcd$return$cursor ; return cursor position in D,E
defw lcd$clear ; clear both lines & home
defw lcd$init ; initialize the display
defw lcd$mode ; set full screen/split screen mode
; --------------------------------------------------------------------
; Lcd print, print A on display following the split/full-screen
; restrictions.
lcd$print: cp CR ; carriage return (no LF) ?
jr nz,trylf
xor a
ld (lcd$column),a ; start at column 0
call lcdcupd ; update the cursor
ret
trylf: cp LF ; line feed?
jr z,yeslf
trydwn: cp CURSDWN ; cursor down?
jr nz,trybs
yeslf: ld a,(split) ; see if split screen
or a
jp nz,upscroll ; yes, just clear current line
ld a,(lcd$line) ; else, ge current full window line
inc a ; go down one line
cp 2 ; see if past bottom line
jr c,lfupd ; cursor still on line 0 or 1, ok
call upscroll ; scroll window up one line
ld a,1 ; set to bottom line
lfupd: ld (lcd$line),a ; update cursor line
jp lcdcupd ; update the cursor position
trybs: cp BS ; back space?
jr z,yesbs
cp CURSLEFT ; cursor left?
jr nz,tryrht
yesbs: ld a,(lcd$column)
or a
ret z ; return if already at left colm
dec a
ld (lcd$column),a
jp lcdcupd ; update the cursor position
tryrht: cp CURSRGHT ; cursor right?
jr nz,tryup
ld a,(lcd$column)
cp chcnt - 1 ; at end of the line?
ret z ; yes, don't move any more
inc a
ld (lcd$column),a
jp lcdcupd ; update the cursor
tryup: cp CURSUP ; cursor up?
jr nz,tryclear
ld a,(split) ; split screen mode?
or a
jp nz,dnscroll ; yes, just clear the current line
ld a,(lcd$line) ; see what line we're on
cp 0
jr z,up41
dec a ; move from line 2 to line 1
ld (lcd$line),a ; update new line
jr up5
up41: call dnscroll ; scroll screen down 1 line
up5: jp lcdcupd ; update the cursor position
tryclear: cp CLEARSCRN ; clear screen
jp z,lcd$clear ; yes, go clear it
display: cp ' '
ret c ; return if control character
cp 07fh
ret nc ; return if >= 7fh
push af ; save the character
ld hl,l1 ; default to line 1 image
ld a,(lcd$line) ; see what line we're on
cp 0 ; line 1?
jr z,yes1 ; yes, skip
ld hl,l2 ; point to line 2 image
yes1: ld d,0
ld a,(lcd$column) ; get cursor column
ld e,a
add hl,de ; compute new buffer address
pop af ; get the character back
ld (hl),a ; update image memory
push af ; save it again
; now update the character to the screen directly
ld a,(lcd$column)
ld b,a ; B = cursor offset from left column of lcd
cp segwid ; > 40 characters?
ld a,lcd$1$ctrl ; default to left lcd segment
jr c,inseg1 ; yes, jump
ld a,b
sub segwid
ld b,a ; B = offset from leftmost lcd column
ld a,lcd$2$ctrl ; in right lcd segment
inseg1: ld (lcd$cr),a ; set up the control register to output to
ld a,(lcd$line) ; then see which line
or a ; top line?
ld c,lcdta ; point to start of top line
jr z,yestop
ld c,lcdba ; else, point to start of bottom line
yestop: ld a,b ; add actual cursor offset from left side
add a,c ; add cursor move command offset
push af
ld a,(lcd$cr) ; set C for the call to the control register
ld c,a
pop af
call outlcd ; and send out the command
ld a,(lcd$cr)
inc a ; make it the Data register
ld c,a ; setup C for the call
pop af ; get back the character
call outlcd ; and send the character to the display
; then update the cursor addess
updcurs: ld a,(lcd$column) ; get cursor column offset
inc a
cp chcnt ; see if past end of line
ret z ; dont update the cursor if eol
ld (lcd$column),a
jp lcdcupd ; update the cursor location
; --------------------------------------------------------------------
; turn lcd display on
lcd$on: ld a,lcddon ; display on command
call sendcmd ; send to both sections
ret
; --------------------------------------------------------------------
; turn both lcds off
lcd$off: ld a,lcddof ; display off command
call sendcmd ; send to both sections
ret
; --------------------------------------------------------------------
; turn lcd cursor on
lcd$on$cursor: jp lcdcupd ; use standard cursor update routine
; --------------------------------------------------------------------
; turn lcd cursor off
lcd$off$cursor: jp curoff ; use standard cursor off routine
; --------------------------------------------------------------------
; move the lcd cursor to x,y = d,e (0-79, 0-1)
lcd$move$cursor:ld a,d
cp chcnt ; > 80 characters?
jr c,mv1 ; no, skip
ld a,chcnt-1 ; default to last column then
mv1: ld (lcd$column),a
ld a,e ; get new line number
cp 2 ; outside of 0-1 range?
jr c,mv2 ; no, skip
ld a,1 ; default to bottom line then
mv2: ld (lcd$line),a
jp lcdcupd ; update the cursor location
; --------------------------------------------------------------------
; Return the address of the LCD cursor position (d=column, e=line)
lcd$return$cursor:
ld a,(lcd$column)
ld d,a
ld a,(lcd$line)
ld e,a
ret
; --------------------------------------------------------------------
; Clear both lines & move cursor home (stays in same line if split mode)
lcd$clear: ld hl,l1 ; clear line 1 buffer
call clear
ld hl,l2 ; clear line 2 buffer
call clear
xor a ; home to current line
ld (lcd$column),a
ld a,(split) ; split screen?
or a
jr nz,clear1 ; yes, dont change line #
ld (lcd$line),a ; init cursor to line 1
clear1: call lcdupd ; and update the display
ret
; --------------------------------------------------------------------
; Initialize LCD to boot up condition
lcd$init: jp lcdint
; --------------------------------------------------------------------
; Set LCD widow mode, 0 (full) or 1 (split screen)
lcd$mode: cp 2 ; only 0-1 allowed
ret nc ; error.. return
ld (split),a ; set new mode
ret
;------------------------------------------------------------------
;
; lcdint:
; the lcdint procedure is the initializing procedure for
; the lcd where the following parameters must be preset:
;
; 1 - display must be reset for both sections
; 2 - display must be cleared for both sections
; 3 - the display turned on for both sections
; 4 - turn on cursor & position it properly
;
;---------------------------------------------------------------------------
lcdint: ld a,lcdrst ; reset lcd section
call sendcmd ; send to both sections
ld a,lcdclr ; clear section
call sendcmd ; send to both sections
ld a,lcddon ; turn display on
call sendcmd ; send to both sections
xor a
ld (split),a ; full screen
ld (lcd$column),a ; init to home postion
ld (lcd$line),a
call lcdcupd ; turn on & position cursor
ld hl,l1
call clear ; clear top line buffer
ld hl,l2
call clear ; clear bottom line buffer
ret
;---------------------------------------------------------------
;
; u p s c r o l l r o u t i n e
;
; the upscroll routine will scroll the display up
; by one line.
;
; algorithm:
;
; procedure scroll window up
; begin
; if split_mode then
; clear current line
; else
; block move line 2 to line 1
; clear bottom line
; end if
; update lcd
; end
;
;---------------------------------------------------------------------
upscroll: ld a,(split) ; obtain the split screen flag
or a
jr nz,up3 ; split screen, jump
; Scroll both lines...
up2: ld hl,l2 ; move line 2 to line 1
ld de,l1
ld bc,chcnt
ldir
ld hl,l2 ; point to top line memory buffer address
call clear ; and clear the line
up4: call lcdupd ; update the lcd screen
ret
; Scroll only the current line (split screen mode)
up3: ld a,(lcd$line) ; get current line
or a
jr nz,onl2
ld hl,l1 ; point to top line memory buffer address
call clear ; and clear the line
ret
onl2: ld hl,l2 ; point to top line memory buffer address
call clear ; and clear the line
ret
;---------------------------------------------------------------------
;
; d n s c r o l l r o u t i n e
;
; the dnscroll routine will cause the virtual window
; to scroll down one line on the lcd.
;
; algorithm:
;
; procedure scroll window down
; begin
; if split_mode then
; clear current line
; else
; block move line 1 to line 2
; clear top line
; end if
; update lcd
; end
;
;-------------------------------------------------------------------------
dnscroll: ld a,(split) ; get split sreen flag
or a ; see if split screen
jr nz,up3 ; split, go back & clear current line
; Scroll both lines...
do2: ld hl,l1 ; move line 1 to line 2
ld de,l2
ld bc,chcnt
ldir
ld hl,l1 ; point to top line memory buffer address
call clear ; and clear the line
do4: call lcdupd ; update the lcd screen
ret
;--------------------------------------------------------------
; Clear 80 characters pointed to by HL to spaces.
; NOTE: B destroyed.
clear: ld b,chcnt ; number of characters per line
ld a,' ' ; clear line to spaces
cl1: ld (hl),a ; and go clear the buffer
inc hl
djnz cl1
ret
;--------------------------------------------------------------
;
; l c d u p d r o u t i n e
;
; the lcdupd routine will update the lcd with the lines
; calculated by the respective routines.
;
; algorithm:
;
; procedure update the lcd
; begin
; lcdout(address l1,lcd top line address start)
; lcdout(address l2,lcd bottom line address start)
; if l1=cursor then lcd cursor := outcur(column,l1)
; else if l2=cursor then lcd cursor:=outcur(column,l2)
; end
;
;----------------------------------------------------------------
lcdupd: call curoff ; turn off cursor
ld hl,l1 ; point to line 1
ld c,lcdta ; address command offset to line1 of lcd (0)
call lcdout ; output data
ld hl,l2 ; point to line 2
ld c,lcdba ; address command offset to line2 of lcd (40)
call lcdout ; output data
call lcdcupd ; update the cursor location
ret
;----------------------------------------------------------------
;
; l c d c u p d r o u t i n e
;
; Update the cursor position to that in the memory image
; (lcd$column, lcd$line)
;
;----------------------------------------------------------------
lcdcupd: ld c,lcdta ; lcd top line address offset
ld a,(lcd$line) ; cursor line no
or a ; test if line 1
jr z,lcdcu1 ; yes, cursor on line 1
ld c,lcdba ; lcd bottom line address offset
lcdcu1: call outcur ; update & turn on cursor
ret
;----------------------------------------------------------------
;
; l c d o u t r o u t i n e
;
; The lcdout routine will take input from the lcd bufer
; indicated by [hl] and output it to the lcd display address
; location indicated by [c]. It effectively updates one line
; of the lcd with its correspondig memory image.
;
; c = 80h or 0c0h always (offset = 0 from start of each 40 column line)
; hl = lcd buffer or lcd buffer + 80
;
; [c] - offset to line 1 or line 2 of each lcd segment
; [hl] - device input start address
;
; algorithm:
;
; procedure lcdout(buffer_addr,display_addr offset)
; begin
; lcd section:=1
; offset:=display_addr
; repeat
; char:=[buffer]
; out_lcd(display_addr,char)
; buffer_addr := buffer_addr + 1
; display_addr := display_addr + 1
; if display_addr - offset = segment width(40) then
; begin
; display_addr:=offset
; lcd section := lcd section + 1
; end
; until lcd section > 2
; end
;
;------------------------------------------------------------------
lcdout: push af
push bc
push de
push hl
ld d,c ; save the lcd addr - offset
ld e,c ; lcd addr will be saved in e
ld a,lcd$1$ctrl ; lcd section 1
ld (lcd$cr),a
ld a,lcd$1$data
ld (lcd$dr),a
out1: ld a,(lcd$cr) ; initialize lcd reg to output
ld c,a
ld a,e ; lcd addr
call outlcd ; set up lcd address
ld a,(lcd$dr) ; data reg
ld c,a
ld a,(hl) ; pick up character from buffer
inc hl ; next char from memory
call outlcd ; output char to lcd
out2: inc e ; next lcd address
ld a,e ; get DISPLAY_ADDR
sub d ; subtract OFFSET
cp segwid ; test if next lcd section(i+1)
jr nz,out3 ; no, then get next char
ld e,d ; set new lcd address - offset
ld a,(lcd$cr) ; next lcd section
add a,2 ; go to section 2
ld (lcd$cr),a
inc a
ld (lcd$dr),a
out3: ld a,(lcd$cr) ; test if end of loop
cp lcd$2$ctrl + 2
jr nz,out1 ; no next char from device
pop hl
pop de
pop bc
pop af
ret
;----------------------------------------------------------------
;
; o u t c u r r o u t i n e
;
; the outcur routine will place the cursor in the proper
; location on the lcd screen.
;
; [c] - lcd line address (080h or 0c0h, for line 1 or line 2 offset)
;
; algorithm:
;
; procedure outcur(column no,lcd line address)
; begin
; lcd section := 1
; if column no > segment width then
; begin
; column no := column no - segment width
; lcd segment := lcd segment +1
; end
; outcur:=column no + lcd line address
; end
;
;---------------------------------------------------------------------
outcur: push af
push bc
push de
ld b,c ; save the lcd line start address
; turn off cursor in both sides ### BUG FIX ###
ld a,lcdcof
call sendcmd
ld a,lcd$1$ctrl ; lcd section 1
ld (lcd$cr),a
ld a,(lcd$column) ; cursor position
ld d,a ; save the cursor position
cp segwid ; test if col no > seg width ## BUG FIX ##
; cp segwid-1 ; test if col no > seg width
jr c,outcxt ; no, get addr offset
sub segwid ; reduce col no
ld d,a ; save new col no
ld a,lcd$2$ctrl ; next lcd section
ld (lcd$cr),a
outcxt: ld a,(lcd$cr) ; set up lcd reg
ld c,a
ld a,b ; lcd line start address
add a,d ; lcd addr for cursor
call outlcd ; put out cursor addr
ld a,lcdcon ; turn on cursor
call outlcd
ld a,lcdcbl ; turn blinking block cursor on
call outlcd
pop de
pop bc
pop af
ret
;--------------------------------------------------------------------
;
; o u t l c d r o u t i n e
;
; Wait loop for the lcd which checks the busy flag in the
; control register indicated by lcd$cr.
;
;-------------------------------------------------------------------
outlcd: push af
push bc
out (c),a
ld a,(lcd$cr)
ld c,a
lwait1: in a,(c)
and busy
jr nz,lwait1
pop bc
pop af
ret
;----------------------------------------------------------------------------
;
; c u r o f f r o u t i n e
;
; the curoff routine will turn off both lcd section cursors
;
;--------------------------------------------------------------------------
curoff: ld a,lcdcof ; cursor off command
jp sendcmd ; send out command
;----------------------------------------------------------------------------
;
; s e n d c m d r o u t i n e
;
; This will send the lcd command in <A> to both LCD control registers
;
;--------------------------------------------------------------------------
sendcmd: push bc
ld c,lcd$1$ctrl ; section 1
call sndbyte ; send first byte
ld c,lcd$2$ctrl ; section 2
call sndbyte ; send second byte
pop bc
ret
sndbyte: push af
ld a,c
ld (lcd$cr),a
pop af
jp outlcd
end
; ---------------------- End Of LCD Driver -----------------------