rendered paste body
RW equ $08 * PB3 is R/W
RS equ $04 * PB2 is RS
E equ $02 * PB1 is E
PORTA equ $0000
PORTB equ $0001
PORTC equ 0x04
DDRA equ $0002
DDRB equ $0003
DDRC equ 0x06
TCNT equ $0084
TSCR equ $0086 make bit7=1 to enable TCNT
Num1 equ $0800
Num1A equ $0801
Num2 equ $0802
Num2A equ $0803
Num3 equ $0804
Num3A equ $0805
Ln1Cnt equ $0806
Ln2Cnt equ $0807
KeyHit equ $0808
equals equ $0809
ycount equ $0810
Math equ $0811
Temp1 equ $0812
Temp2 equ $0813
Temp3 equ $0814
org $0819
buf rmb 10 ; characters typed
cnt rmb 1 ; loop counter, number of keys left to be typed
org $F000 Object code goes in ROM
***************LCDInit***********************
* Initialize HD44780 LCD display
* Inputs: none Outputs: none
* Registers modified: A,CCR
LCDInit
ldaa #$FF
staa DDRA Output to LCD data
ldaa #$FF
staa DDRB Outputs pb1=E, pb2=RS, pb3=R/W
clr PORTB E=0, RS=0, R/W=0
rts
***************LCDOutChar***********************
* Output one character to HD44780 LCD display
* Inputs: RegA is ASCII character Outputs: none
* Registers modified: B,CCR
LCDOutChar
staa PORTA
ldab #E+RS E=1, RS=1, R/W=0
stab PORTB
ldab #RS E=0, RS=1, R/W=0
stab PORTB
bsr LCDWaitShort 40us wait
rts
***************LCDOutCmd***********************
* Output command to HD44780 LCD display
* Inputs: RegA is LCD command Outputs: none
* Registers modified: B,CCR
* Entry Mode Set 0,0,0,0,0,1,I/D,S
* I/D=1 for increment cursor move direction
* =0 for decrement cursor move direction
* S =1 for display shift
* =0 for no display shift
* Display On/Off Control 0,0,0,0,1,D,C,B
* D =1 for display on
* =0 for display off
* C =1 for cursor on
* =0 for cursor off
* B =1 for blink of cursor position character
* =0 for no blink
* Cursor/Display Shift 0,0,0,1,S/C,R/L,*,*
* S/C=1 for display shift
* =0 for cursor movement
* R/L=1 for shift to left
* =0 for shift to right
* Function Set 0,0,1,DL,N,F,*,*
* DL=1 for 8 bit
* =0 for 4 bit
* N =1 for 2 lines
* =0 for 1 line
* F =1 for 5 by 10 dots
* =0 for 5 by 7 dots
LCDOutCmd
staa PORTA
ldab #E E=1, RS=0, R/W=0
stab PORTB
ldab #0 E=0, RS=0, R/W=0
stab PORTB
bsr LCDWaitLong 1.54ms wait
rts
***************LCDWaitShort***********************
* 20us wait HD44780 LCD display
* real display would require 37us
* Inputs: none Outputs: none
* Registers modified: B,CCR
LCDWaitShort
ldd #1
*ldd #360 8MHz
addd TCNT TCNT value at the end of the delay
sloop cpd TCNT EndT-TCNT<0 when EndT<Tcnt
bpl sloop
rts
***************LCDWaitLong***********************
* 50us wait HD44780 LCD display
* real display would require 1.54ms
* Inputs: none Outputs: none
* Registers modified: B,CCR
LCDWaitLong
ldd #4
*ldd #12400 8MHz
addd TCNT TCNT value at the end of the delay
lloop cpd TCNT EndT-TCNT<0 when EndT<Tcnt
bpl lloop
rts
******Initialize Keyboard*******
*Inputs: None
*Outputs: None
Key_Init movb #$00,DDRC
movb #$00,PORTC
rts
******Input from Keyboard*******
*waits for exactly one key to be pressed
*Inputs: none
*Outputs: RegA is ASCII code of key typed
Key_In
movb #$80,DDRC ; activate row0
ldaa PORTC
anda #$0F
cmpa #$07 ;"1" pressed?
bne not1
ldaa #$01
staa Num1
ldaa #'1'
jmp found
not1 cmpa #$0B ;"2" pressed?
bne not2
ldaa #$02
staa Num1
ldaa #'2'
jmp found
not2 cmpa #$0D ;"3" pressed?
bne not3
ldaa #$03
staa Num1
ldaa #'3'
jmp found
not3 cmpa #$0E ;"+" pressed?
bne notadd
ldaa #$2B
jmp MathSym
notadd movb #$40,DDRC ; x0xxx activates row1
ldaa PORTC
anda #$0F
cmpa #$07 ;"4" pressed?
bne not4
ldaa #$04
staa Num1
ldaa #'4'
jmp found
not4 cmpa #$0B ;"5" pressed?
bne not5
ldaa #$05
staa Num1
ldaa #'5'
jmp found
not5 cmpa #$0D ;"6" pressed?
bne not6
ldaa #$06
staa Num1
ldaa #'6'
jmp found
not6 cmpa #$0E ;"-" pressed?
bne notsub
ldaa #$2D
jmp MathSym
notsub movb #$20,DDRC ; xx0x activates row2
ldaa PORTC
anda #$0F
cmpa #$07 ;"7" pressed?
bne not7
ldaa #$07
staa Num1
ldaa #'7'
bra found
not7 cmpa #$0B ;"8" pressed?
bne not8
ldaa #$08
staa Num1
ldaa #'8'
bra found
not8 cmpa #$0D ;"9" pressed?
bne not9
ldaa #$09
staa Num1
ldaa #'9'
bra found
not9 cmpa #$0E ;"*" pressed?
bne notmult
ldaa #$2A
jmp MathSym
notmult movb #$10,DDRC ; xxx0 activates row3
ldaa PORTC
anda #$0F
cmpa #$07 ;"c" pressed?
bne notc
jsr Clear
jmp Key_In
notc cmpa #$0B ;"0" pressed?
bne not0
ldaa #$00
staa Num1
ldaa #'0'
bra found
not0 cmpa #$0D
bne notequ
jsr EquCmd
jmp Key_In
notequ cmpa #$0E ;"/" pressed?
bne notdiv
ldaa #$2F
bra MathSym
notdiv jmp Key_In ; wait for exactly one
MathSym staa Math
ldy #$00
movb Num1,Num3
ldaa #$01
jsr LCDOutCmd
movb #$80,Ln1Cnt Sets counter line 1
movb #$C0,Ln2Cnt Sets counter line 2
jmp Key_In
found staa KeyHit
jsr LCDOutChar
iny
rts
******wait for release
*inputs: none
*outputs: none
Key_Release
movb #$F0,DDRC ; activate all rows
wait ldaa PORTC
anda #$0E
cmpa #$0E ; none pressed?
bne wait
rts
*Clears out display and variables*
Clear
ldaa #$01
jsr LCDOutCmd
clr KeyHit
clr Num1
clr Num2
clr Num3
movb #$80,Ln1Cnt Sets counter line 1
movb #$C0,Ln2Cnt Sets counter line 2
rts
*End clear command.*
*Add Command.*
AddCmd
ldaa Num1
adda Num2
cmpa #$FF
bgt Error2
staa Num1
rts
Error2 jsr ErrorMsg
rts
*End of Add command.*
*Subtract command.*
SubCmd
ldaa Num1
suba Num2
cmpa #$00
blt Error3
staa Num1
rts
Error3 jsr ErrorMsg
rts
*End of Subtract command.*
*Multiply command.*
MultCmd
ldaa Num1
ldab Num2
mul
cmpb #$FF
bgt Error4
stab Num1
rts
Error4 jsr ErrorMsg
rts
*End of Multiply command.*
*Divide command.*
DivCmd
ldd Num1
ldx Num2
cpx #$00
beq Error5
idiv
xgdx
stab Num1
rts
Error5 jsr ErrorMsg
rts
*End of Divide command.*
*Outputs answer to the lcd screen.*
EquCmd
movb Num1,Num2
movb Num3,Num1
ldaa Math
cmpa #$2B
beq Add
cmpa #$2D
beq Sub
cmpa #$2A
beq Mul
cmpa #$2F
beq Div
rts
Add jsr AddCmd
jsr Print
rts
Sub jsr SubCmd
jsr Print
rts
Mul jsr MultCmd
jsr Print
rts
Div jsr DivCmd
jsr Print
rts
*End of Equales Ccmmand.*
*Prints answer to lcd*
Print
rts
*Finish Print command*
*Outputs Error message to lcd*
ErrorMsg
movb #$80,Ln1Cnt Sets counter line 1
movb #$C0,Ln2Cnt Sets counter line 2
ldy #ErrMsg
OutMsg ldaa Ln1Cnt ; moves cursor back to 1st line
jsr LCDOutCmd ; Displays cursor on 1st line
ldaa 0,y
jsr LCDOutChar
ldaa Ln2Cnt ; moves cursor to 2nd line
jsr LCDOutCmd ; Displays cursor on 2nd line
ldaa 0,y
jsr LCDOutChar
inc Ln2Cnt ; Increments line 2 counter
inc Ln1Cnt ; Increments line 1 counter
ldaa Ln1Cnt ; moves cursor back to 1st line
jsr LCDOutCmd ; Displays cursor on 1st line
iny
ldaa 0,y
cmpa #$00
bne OutMsg
jsr LCDWaitLong
jsr Clear
rts
*End Error message command*
main lds #$0C00 stack grows down from $0BFF
ldaa #$80
staa TSCR enable TCNT
jsr LCDInit
Loop ldaa #$01 clear
jsr LCDOutCmd
ldaa #$38 N=1 two line, F=0 5 by 7 dots
jsr LCDOutCmd DL=1 8-bit data
ldaa #$0E set D=1, C=1, B=0
jsr LCDOutCmd display on, cursor on, blink off
ldaa #$06 set I/D, S
jsr LCDOutCmd increment, no shift
lds #$0c00
ldx #buf
movb #10,cnt
jsr Key_Init
ldy #$00
movb #$80,Ln1Cnt Sets counter line 1
movb #$C0,Ln2Cnt Sets counter line 2
scan jsr Key_In
ldaa Ln2Cnt ; moves cursor to 2nd line
jsr LCDOutCmd ; Displays cursor on 2nd line
ldaa KeyHit
jsr LCDOutChar
inc Ln2Cnt ; Increments line 2 counter
inc Ln1Cnt ; Increments line 1 counter
ldaa Ln1Cnt ; moves cursor back to 1st line
jsr LCDOutCmd ; Displays cursor on 1st line
staa 1,x+ ; save
dec cnt ; quit after 10
beq done
jsr Key_Release
bra scan
done stop
bra Loop
ErrMsg fcc "Error"
fcb 0
org $FFFE
dc.w main Starting address after a RESET
end