summaryrefslogtreecommitdiff
path: root/cmpen472hw5_McDonnell/Sources/main.asm
blob: 06b44c8e7771d9d2daa73d0e2123f9091fa58b69 (plain)
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
**************************************************************************
*
* Title:        LED Light Dimmer Loop
*
* Objective:    CMPEN 472 Homework 4
*
* Revision:     V1.0
*
* Date:         Feb. 14, 2025
*
* Programmer:   Jacob McDonnell
*
* Company:      The Pennsylvania State University
*               Department of Computer Science and Engineering
*
* Algorithm:    Simple Parallel I/O use, time delay-loop, and PWM control
*
* Register Use: A to control LEDS initially and for Light Level
*               X to hold the counter in the loop.
*
* Memory Use:   RAM Locations from $3000 for data,
*               RAM Locations from $3100 for program
*
* Input:        Parameters hard-coded in the program - PORTB
*               Switch 1 at PORTB bit 0
*               Switch 2 at PORTB bit 1
*               Switch 3 at PORTB bit 2
*               Switch 4 at PORTB bit 3
*
* Output:       LED 1 at PORTB bit 4
*               LED 2 at PORTB bit 5
*               LED 3 at PORTB bit 6
*               LED 4 at PORTB bit 7
*
* Observation:  This program will decrease LED 4 from 100% to 0% in 400ms
*               and then increase LED 4 from 0% to 100% in 400ms. The program
*               will loop forever.
*
* Note:         ON CSM-12C128 board,
*               Switch 1 is at PORTB bit 0, and
*               LED 4 is at PORTB bit 7.
*
* Comments:     This program is developed and simulated using CodeWarrior
*               development software and targeted for Axion
*               Manufacturing's CSM-12C128 board running at 24MHz.
*
**************************************************************************
* Parameter Declearation Section
*
* Export Symbols
            xdef        pgstart         ; export 'pgstart' symbol
            absentry    pgstart         ; for assembly entry point

* Symbols and Macros
PORTA       equ         $0000           ; i/o port A addresses
DDRA        equ         $0002           ; data direction register for PORTA
PORTB       equ         $0001           ; i/o port B addresses
DDRB        equ         $0003           ; data direction register for PORTB

SCIBDH      equ         $00C8           ; Serial port (SCI) Baud Register H
SCIBDL      equ         $00C9           ; Serial port (SCI) Baud Register L
SCICR2      equ         $00CB           ; Serial port (SCI) Control Register 2
SCISR1      equ         $00CC           ; Serial port (SCI) Status Register 1
SCIDRL      equ         $00CF           ; Serial port (SCI) Data Register

CR          equ         $0d             ; carriage return, ASCII 'Return' key
LF          equ         $0a             ; line feed, ASCII 'next line' character

**************************************************************************
* Data Section: address used [ $3000 to $30FF ] RAM Memory
*
            org         $3000           ; Reserved RAM memory starting address
                                        ;   for Data for CMPEN 472 class
Counter     dc.w        $0036           ; X register count number for time Delay
                                        ;   loop for 10 useconds
                                        ; The work to calculate this number is in
                                        ; the comments for the delay10usec subroutine.

LEVEL       dc.b        $0005           ; Light Level that the LED should be

*
**************************************************************************
* Program Section: address used [ $3100 to $3FFF ] RAM Memory
*
            org         $3100           ; Program start address, in RAM
pgstart     lds         #$3100          ; initialize the stack pointer

            ldaa        #%11110001      ; LED 1,2,3,4 at PORTB bit 4,5,6,7
            staa        DDRB            ; set PORTB bit 4,5,6,7 as output

mainLoop
            bra         mainLoop        ; Loop back to mainLoop always

**************************************************************************
* Subroutine Section: address used [ $3100 to $3FFF ] RAM Memory
*

;*************************************************************************
; WriteString subroutine
;
; This subroutine will write a given null terminated string to the serial.
;
; Input:                   Address of null terminated string in X
; Output:                  Null terminated string written to serial
; Registers in use:        X for the address of the string and A for the current byte
; Memory locations in use: Memory Address for serial line, address of the string
;
; Comments: This subroutine requires serial to be setup and putchar subroutine.
;

WriteString
            psha                        ; Save A to the stack
writeLoop   ldaa       1,x+             ; Load the byte at addr in X, then add 1
            beq        doneWrite        ; if A == 0, branch to doneWrite
            jsr        putchar          ; Jump to putchar to write byte to serial
            bra        writeLoop        ; branch always to writeLoop
doneWrite   pula                        ; restore A from the stack
            rts                         ; return to caller

;*************************************************************************
; TurnOnLED subroutine
;
; This subroutine will dim turn on a specified LED
;
; Input:                   LED number 1 to 3, in accumulator A
; Output:                  Specified LED turned on
; Registers in use:        A accumulator to specify the LED
; Memory locations in use: PORTB memory location associated with PORTB on the chip
;
; Comments: This subroutine requires PORTB to be setup
;

TurnOnLED
           cmpa         #1              ; Compare A to 1
           bne          onCheckTwo      ; If A != 1, check next number
           bset         PORTB,%00010000 ; Turn On LED 1
           bra          onDone          ; Jump to onDone
onCheckTwo cmpa         #2              ; Compare A to 2
           bne          onCheckThr      ; If A != 2, check next number
           bset         PORTB,%00100000 ; Turn On LED 2
           bra          onDone          ; Jump to onDone
onCheckThr bset         PORTB,%01000000 ; Turn On LED 3
onDone     rts                          ; Return to caller

;*************************************************************************
; TurnOffLED subroutine
;
; This subroutine will dim turn of a specified LED
;
; Input:                   LED number 1 to 3, in accumulator A
; Output:                  Specified LED turned of
; Registers in use:        A accumulator to specify the LED
; Memory locations in use: PORTB memory location associated with PORTB on the chip
;
; Comments: This subroutine requires PORTB to be setup
;

TurnOffLED
           cmpa         #1              ; Compare A to 1
           bne          ofCheckTwo      ; If A != 1, check next number
           bclr         PORTB,%00010000 ; Turn Off LED 1
           bra          ofDone          ; Jump to onDone
ofCheckTwo cmpa         #2              ; Compare A to 2
           bne          ofCheckThr      ; If A != 2, check next number
           bclr         PORTB,%00100000 ; Turn Off LED 2
           bra          ofDone          ; Jump to onDone
ofCheckThr bclr         PORTB,%01000000 ; Turn Off LED 3
ofDone     rts                          ; Return to caller

;*************************************************************************
; HighToLow subroutine
;
; This subroutine will dim LED4 from 100% to 0% in 400ms
;
; Input:                   No Input, all parameters are hard coded
; Output:                  LED4 dimmed from 100% to 0% in 400ms, wasted cycles
; Registers in use:        A accumulator to control the light level of the LED
; Memory locations in use: PORTB memory location associated with PORTB on the chip
;
; Comments: This subroutine requires dimmer subroutine
;

HighToLow
            psha                        ; Save accumulator A to the stack
            ldaa        #100            ; load 100 into accumulator A
decrease    tbeq        A,doneDec       ; Test if A == 0, skip loop if so
            staa        LEVEL           ; Save A to LEVEL
            jsr         dimmer          ; jump to dimmer subroutine
            jsr         dimmer          ; jump to dimmer subroutine
            jsr         dimmer          ; jump to dimmer subroutine
            jsr         dimmer          ; jump to dimmer subroutine
            deca                        ; decrement accumulator A by 1
            bra         decrease        ; loop to decrease always
doneDec     pula                        ; Restore A from the stack
            rts                         ; Return to caller

;*************************************************************************
; HighToLow subroutine
;
; This subroutine will dim LED4 from 100% to 0% in 400ms
;
; Input:                   No Input, all parameters are hard coded
; Output:                  LED4 dimmed from 100% to 0% in 400ms, wasted cycles
; Registers in use:        A accumulator to control the light level of the LED
; Memory locations in use: PORTB memory location associated with PORTB on the chip
;
; Comments: This subroutine requires dimmer subroutine
;

LowToHigh
            psha                        ; Save accumulator A to the stack
            ldaa        #0              ; load 100 into accumulator A
increase    cmpa        #100            ; Compare A to 100
            beq         mainLoop        ; Test if A == 100, jump to mainLoop
            staa        LEVEL           ; Save A to LEVEL
            jsr         dimmer          ; jump to dimmer subroutine
            jsr         dimmer          ; jump to dimmer subroutine
            jsr         dimmer          ; jump to dimmer subroutine
            jsr         dimmer          ; jump to dimmer subroutine
            inca                        ; increment accumulator A by 1
            bra         increase        ; loop to increase always
doneInc     pula                        ; Restore A from the stack
            rts                         ; Return to caller

;*************************************************************************
; dimmer subroutine
;
; This subroutine will dim LED4 to a given level
;
; Input:                   Two 1 byte counters, ONN and OFF, for how many times
;                          LED4 should be on and off for.
; Output:                  LED4 dimmed to a given level, wasted cycles
; Registers in use:        A accumulator to counter number of times looped
; Memory locations in use: Two bytes ONN and OFF used to dim the LED4 to a given level
;
; Comments: This subroutine requires delay10usec subroutine
;

dimmer
            bset        PORTB,%10000000 ; Turn LED4 on
            psha                        ; Save A to the stack
            ldaa        LEVEL           ; Load the light level into accumulator A
onDelay     tbeq        A, skipToOff    ; Test if A == 0, skip loop if so
            jsr         delay10usec     ; delay for 10 microseconds
            deca                        ; decrement accumulator A by 1
            bra         onDelay         ; jump back to onDelay always
skipToOff   bclr        PORTB,%10000000 ; Turn off LED4
            ldaa        #100            ; load 100 into accumulator A
            suba        LEVEL           ; Subtract LEVEL to get off count
offDelay    tbeq        A,doneLoop      ; Test if A == 0, skip loop if so
            jsr         delay10usec     ; delay 10 microseconds
            deca                        ; decrement accumulator A by 1
            bra         offDelay        ; jump back to offDelay always
doneLoop    pula                        ; restore A from the stack
            rts                         ; return to caller

;*************************************************************************
; delay10usec subroutine
;
; This subroutine causes a 10 usec. delay
;
; Input:                   a 16bit count number in 'Counter'
; Output:                  time delay, cpu cycle wasted
; Registers in use:        X register, as counter
; Memory locations in use: a 16bit input number at 'Counter'
;
; Comments: Code relies on counter being $39 to be exactly 10 usec work is below
; Given: freq = 24MHz = 24000000     sec = 10 usec = 0.00001
; freq = cycles / seconds
;
; cycles = freq * seconds = 24000000Hz * 0.00001 = 240
;
; This sub routine is 12 + 4 * 'Counter' cycles long, solving for 'Counter'
; the result is found to be 57.
;

delay10usec
            pshx                        ; Save register x to the stack
            ldx         Counter         ; load counter into register x
innerLoop   dex                         ; decrement register x by 1
            bne         innerLoop       ; loop while register x is not 0
            pulx                        ; restore register x from the stack
            nop                         ; extra nop to make exactly 10 usec
            rts                         ; return to caller

;***************putchar************************
;* Program: Send one character to SCI port, terminal
;* Input:   Accumulator A contains an ASCII character, 8bit
;* Output:  Send one character to SCI port, terminal
;* Registers modified: CCR
;* Algorithm:
;    Wait for transmit buffer become empty
;      Transmit buffer empty is indicated by TDRE bit
;      TDRE = 1 : empty - Transmit Data Register Empty, ready to transmit
;      TDRE = 0 : not empty, transmission in progress
;**********************************************
putchar        brclr SCISR1,#%10000000,putchar   ; wait for transmit buffer empty
               staa  SCIDRL                      ; send a character
               rts
;***************end of putchar*****************


;****************getchar***********************
;* Program: Input one character from SCI port (terminal/keyboard)
;*             if a character is received, other wise return NULL
;* Input:   none
;* Output:  Accumulator A containing the received ASCII character
;*          if a character is received.
;*          Otherwise Accumulator A will contain a NULL character, $00.
;* Registers modified: CCR
;* Algorithm:
;    Check for receive buffer become full
;      Receive buffer full is indicated by RDRF bit
;      RDRF = 1 : full - Receive Data Register Full, 1 byte received
;      RDRF = 0 : not full, 0 byte received
;**********************************************
getchar        brclr SCISR1,#%00100000,getchar7
               ldaa  SCIDRL
               rts
getchar7       clra
               rts
;****************end of getchar****************

            end                         ; last line of the file