Rotate - Conditional and Io
Rotate - Conditional and Io
Shifts
Example: LSL
D0 = $000000AA D1 = $00000008 LSL.W D1,D0
0000 0000 1010 1010 D0.W = 0 0 A A 0000 0000 0 0
Pushed out
LSL
D0.W =
Example: LSR
D0 = $000000AA LSR.W #4,D0
D0.W = 0 0 A A
D0.W =
Example: ROL
D0 = $000000AB D1 = $00000003 ROL.B D1,D0
D0.B = 1 0 1 0 1 0 1 1
And sent to back
D0.B =
Pushed out
D0.B =
New D0 = $0000005D
Example: ROR
D0 = $000000AB ROR.B #4,D0
D0.B = 1 0 1 0 1 0 1 1
D0.B =
Pushed out
D0.B =
New D0 = $000000DA
Branch Instructions
Branch Instructions
Branch instructions are instructions that transfer execution of the program to another location. The new location is marked by a label. The format is BRA <label>.
In BRA, the program will branch unconditionally when it encounters the BRA command. A branch instruction may also be given a condition:
Called Bcc (Branch when condition is true). If the tested condition is true, then branch to <label>. If the tested condition is not true, then just execute
next line.
Bcc Flowchart
START
TRUE
Condition = TRUE?
FALSE
Go to LABEL
FINISH
Conditions (cc)
CMP.B D1,D0
D0.B > D1.B D0.B D1.B D0.B = D1.B D0.B D1.B D0.B D1.B D0.B < D1.B
Bcc Format
LABEL Instructions #1 Instructions #2 Instructions #n Test Condition (CMP) Bcc LABEL
If conditions = FALSE
If condition = TRUE
Bcc Format
If conditions = TRUE, Instructions 1 n will not be executed
Test Condition (CMP) Bcc LABEL Instructions #1 If conditions = FALSE, Instructions #2 Instructions after LABEL executed line-by-line. Instructions #n Instructions after LABEL
LABEL
IfElse
the condition is true, execute a block of statements. Else, execute another block of statements.
Check the byte stored in D3. If D3 > 10, clear the byte. Else, add 3 to the value stored in D3.
Flowchart
START Move value to D3 True False
D3 > 10?
Clear D3
D3 = D3 + 3
FINISH
Assembly Code
START ORG $1000
MOVE.B
CMP.B BGT BLE MORE CLR.B BRA ADD.B
#11,D3
#10,D3 MORE LESS D3 FINISH #3,D3
D3.B < 10
D3.B > 10
LESS
FINISH
END
START
Check the value of 1 byte stored at memory location $2000. If the value is equal to zero, then replace it with $FF. Else, do not change its value.
Flowchart
Program
START ORG
MOVE.B CMP.B BEQ BNE ISZERO MOVE.B BRA
$1000
$2000,D0 #0,D0 ISZERO NOTZERO #$FF,$2000 HABIS
NOTZERO
HABIS
NOP
END START
(NO OPERATION)
Flowchart
Flowchart
Graphical method to plan flow of our programs. Shows programs step-by-step operation. Easy to understand and analyze. Can be used to write organized programs.
Flowchart
Basic shapes:
Terminator. Process.
Decision.
Input/Output. Connectors.
START FINISH
Short
Example:
A=A+B
Reset Ports
Clear D0
Examples:
Port is active?
TRUE
D0 = 3?
FALSE
TRUE
FALSE
1 3
Example: Connector
START 1
Input D0 D0 = D0 x D1
Input D1
FINISH 1
Example 1
Example 1
START 1
D1 D0
Input D0 D0 = D0 x D1
Write a program that calculates the area of a rectangle using M68000 assembly language. The length and width of the rectangle is stored in D0 and D1.
Input D1
FINISH 1
Translation to Assembly
START START ORG $1000
Input D0
MOVE.W
#$0012,D0
Input D1
MOVE.W
#$0034,D1
D0 = D0 x D1
MULU
D1,D0
FINISH
END
START
Complete Program
START ORG
MOVE.W MOVE.W
$1000
#$0012,D0 #$0034,D1
MULU
END
D1,D0
START
Example 2
Example 2
Two numbers are stored in memory locations $5000 (A) and $6000 (B). If A is larger than B, then move A to memory location $7000. If B is larger than A, then move B to memory location $7000.
TRUE
A > B?
FALSE
STORE A AT $7000
STORE B AT $7000
SIMPANA SIMPANB
FINISH
HABIS
END
START
Example 3
Example 3
Memory locations $1100 to $1109 contains 10 bytes of data. Write a program that adds together the 10 bytes and stores them in D1.
Flowchart
START
TRUE
D0 = 0?
FALSE
FINISH
D0 = 10
D0 = D0 - 1 Clear D1
START
ORG MOVE.B MOVE.B MOVE.B MOVE.B MOVE.B MOVE.B MOVE.B MOVE.B MOVE.B MOVE.B
$1000 #$11,$1100 #$22,$1102 #$33,$1104 #$44,$1106 #$55,$1108 #$66,$110A #$77,$110C #$88,$110E #$99,$1110 #$11,$1112
MOVE.L
#$001100,A0
D0 = 10
MOVE.B
#10,D0
Clear D1
D0 = 0?
FALSE
CLR.B
D1
D1 = D1 + (A0)
Go to next memory location ADD.B ADD.L (A0),D1 #1,A0
D0 = D0 - 1
FINISH
SUB.B END
#1,D0
Complete Program
START ORG MOVE.B MOVE.B MOVE.B MOVE.B MOVE.B MOVE.B MOVE.B MOVE.B MOVE.B MOVE.B $1000 #$11,$1100 #$22,$1102 #$33,$1104 #$44,$1106 #$55,$1108 #$66,$110A #$77,$110C #$88,$110E #$99,$1110 #$11,$1112 MOVE.L #$1100,A0 MOVE.B #10,D0 CLR.B D1 LOOP ADD.B ADD.L SUB.B CMP.B BNE END (A0),D1 #1,A0 #1,D0 #0,D0 LOOP START
I/O Interfacing
I/O Interfacing
M68k cannot control the devices directly, it has to use the M68230 I/O chip to control the devices.
M68230 Interfacing
Memory
Registers in M68230
M68230 contains 23 registers. Before using the M68230, the M68230 needs to be initialized. Initialization is done by configuring certain registers in the M68230:
Port
General Control Register. Port X Control Register (A, B). Port X Data Direction Register (A, B, C). Port X Data Register (A, B, C).
PCDDR
PACR PBCR PADR
$800009
$80000D $80000F $800011
PBDR
PCDR
$800013
$800019
To set the M68230 to device bit I/O mode, the settings are:
Set
PGCR to $00. Set PACR and PBCR to $80. Set PADDR and PBDDR based on data direction.
For input devices, set bit to 0. For output devices, set bit to 1.
M68230 Initialization
PGCR PADDR PBDDR PCDDR PACR PBCR PADR PBDR PCDR EQU EQU EQU EQU EQU EQU EQU EQU EQU
MOVE.B MOVE.B MOVE.B
The DDR is set depending on whether the device is an input or output device.
Output
device, set to 1. Example: LED, 7Segment. Input device, set to 0. Example: Switches, keypad.
Example: LED
M68230 PB0 PB1
PB2
PB3 PB4 PB5 PB6 PB7
MOVE.B
#$FF,PBDDR
PA2
R
PA3
R
LED3
LED4 LED5
PA4
R
PA5
R
PB5
PB6 PB7
PA6
R
LED6
LED7
PA7
R
Example: 7-Segment
M68230
a B0 b B1 c B2 B3 B4 B5 B6 B7 d e f g
MOVE.B
#$FF, PBDDR
B2
B3 B4 B5 B6 B7
d e f g
MOVE.B
#$0F,PBDDR
PB2
PB3
M68230 PA0 PB0 PA1 PB1 PB2 PB3 PB4 PB5 PB6 PB7
PA2
R
If a switch is closed (activated), it will generate a 1 inside the PXDR. In this example, the data from switches is stored inside D0 in the M68000 data register. MOVE.B PADR,D0
PA3
R
PA4
R
PA5
R
Using this command, the value inside D0 will be: D0 = $00000000 D0 = $000000C3
PA6
R
PA7
R
7-Segment
Consists of 7-LEDs arranged together. Can display numbers and characters. Each segment is marked with a letter (a to g). To display characters, need to turn on/off certain segments.
Displaying Numbers
a 1 b 1 c 0 d 0 e 0 f 0 g 0 Number 1
1
1 0 1 0 1 1 1 1
1
1 1 0 0 1 1 1 1
0
1 1 1 1 1 1 1 1
1
1 0 1 1 0 1 0 1
1
0 0 0 1 0 1 0 1
0
0 1 1 1 0 1 1 1
1
1 1 1 1 0 1 1 0
2
3 4 5 6 7 8 9 0
In this example, to display 4 on the 7-segment, b, c, e, f, and g must be turned on, and the rest turned off. This can be done by sending $66 to the PBDR.
MOVE.B
#$ 66, PBDR
The 7448 decoder can simplify the transfer of data to the 7-segment. It converts BCD values directly into the necessary signals to turn on the 7-segment. If the decoder is present, there is no need to consider the values of a-b-c-.-g, just send the number to be displayed and it is automatically shown.
B2
B3 B4 B5 B6 B7
d e f g
Displaying Numbers
A3 0
a
A2 0 0 0 1
A1 0 1 1 0
A0 1 0 1 0
Number 1 2 3 4
0 0 0
A3 A2 A1 A0
b c
d e f g
0 0 0 1 1 0
1 1 1 0 0 0
0 1 1 0 0 0
1 0 1 0 1 0
5 6 7 8 9 0
Sample Programs
Example 1
PB2
PB3 PB4 PB5 PB6 PB7
PBCR
PADDR PBDDR PCDDR
$800007
$800009 $80000D $80000F
LED6
LED7
PADR
PBDR PCDR
$800011
$800013 $800019
Discussion
Port B should be initialized before being used. To turn on LED, the voltage at Port B bits should be high. To turn off LED, the voltage at Port B should be low.
Flowchart
Solution
START PGCR PBCR PBDDR PBDR
INIT
ONLED
PB2
PB3 PB4 PB5 PB6 PB7
Example 2
A set of switches are connected to Port A, and a set of LEDs are connected to Port B in M68230. Write a program that reads the value in the switches and turns on the respective LEDs.
Circuit Diagram
+5V M68230 PA0 PB0
R
PA1
R
PA2
R
PA3
R
LED3
LED4 LED5
PA4
R
PA5
R
PB5
PB6 PB7
PA6
R
LED6
LED7
PA7
R
Port Assignments
Port
PGCR PACR PBCR
Address
$A00001 $A0000D $A0000F
PADDR
PBDDR PCDDR PADR PBDR PCDR
$A00005
$A00007 $A00009 $A00011 $A00013 $A00019
Discussion
Both Port A & B should be initialized before being used. When the switch is ON, 5 V is passed to M68230 (logic 1). When the switch is OFF, 0 V is passed to M68230 (logic 0).
Flowchart
LOOP
Sample Output
+5V M68230 PA0 PB0
R
PA1
R
PA2
R
PA3
R
LED3
LED4 LED5
PA4
R
PA5
R
PB5
PB6 PB7
PA6
R
LED6
LED7
PA7
R
Example 3
Example 1
The M68230 is connected to the hardware shown. Write a program that turns on all LEDs when both switches are closed. Else, turn off all LEDs. The port assignments are shown below:
Port PGCR PACR PBCR PADDR PBDDR PCDDR PADR PBDR PCDR Address $A00001 $A0000D $A0000F $A00005 $A00007 $A00009 $A00011 $A00013 $A00019
Flowchart
Program
START PGCR PACR PBCR PADDR PBDDR PADR PBDR ORG EQU EQU EQU EQU EQU EQU EQU $1000 $A00001 $A0000D $A0000F $A00005 $A00007 $A00011 $A00013 READSW MOVE.B MOVE.B AND.B CMP.B BEQ BNE MOVE.B BRA MOVE.B #$00,PBDR PADR,D0 #$C0,D0 #$C0,D0 ONLED OFFLED #$FF,PBDR HABIS #$00,PBDR
ONLED
INIT
OFFLED HABIS
END
START
Example 4
Example: 7-Segment
M68230
a A0 b A1 c A2 A3 A4 A5 A6 A7 d e f g
Flowchart
Program
PGCR PADDR PBDDR PCDDR PACR PBCR PADR PBDR PCDR EQU EQU EQU EQU EQU EQU EQU EQU EQU MOVE.B MOVE.B MOVE.B MOVE.B END $800001 $800005 $800007 $800009 $80000D $80000F $800011 $800013 $800019 #$00,PGCR #$80,PACR #$FF,PADDR #$79,PADR START
Example 5
Flowchart
Program
START ORG
PGCR PADDR PBDDR PCDDR PACR PBCR PADR PBDR PCDR EQU EQU EQU EQU EQU EQU EQU EQU EQU
$1000
$800001 $800005 $800007 $800009 $80000D $80000F $800011 $800013 $800019
Example 6
Example 6
The M68230 is connected to the hardware shown. Write a program that tests the switch at Port A. If the switch is turned on, display 3 at the 7-segment, and turn on the LED at Port B. The port assignments are shown below:
Port PGCR PACR PBCR PADDR PBDDR PCDDR PADR PBDR PCDR Address $800001 $80000D $80000F $800005 $800007 $800009 $800011 $800013 $800019
Flowchart
Program
START PGCR PACR PBCR PADDR PBDDR PADR PBDR ORG EQU EQU EQU EQU EQU EQU EQU $1000 $800001 $80000D $80000F $800005 $800007 $800011 $800013 READSW MOVE.B MOVE.B AND.B CMP.B BEQ BNE #$00,PBDR PADR,D0 #$02,D0 #$02,D0 ON7SEG OFF7SEG #$31,PBDR HABIS #$00,PBDR
ON7SEG
INIT
END
START
Example 7
Example 7
The M68230 is connected to the hardware shown. Write a program that continuously tests the switches at Port A and displays the corresponding value at the 7-segment. The port assignments are shown below:
Port PGCR PACR PBCR PADDR PBDDR PCDDR PADR PBDR PCDR
Address $800001 $80000D $80000F $800005 $800007 $800009 $800011 $800013 $800019
Flowchart
Program
START PGCR PACR PBCR PADDR PBDDR PADR PBDR ORG EQU EQU EQU EQU EQU EQU EQU $1000 $800001 $80000D $80000F $800005 $800007 $800011 $800013 CMP.B BEQ CMP.B BEQ CMP.B BEQ CMP.B BEQ BRA DISP0 DISP1 DISP2 DISP3 MOVE.B BRA MOVE.B BRA MOVE.B BRA MOVE.B BRA END #$00,D0 DISP0 #$01,D0 DISP1 #$02,D0 DISP2 #$03,D0 DISP3 ULANG #$00,PBDR ULANG #$01,PBDR ULANG #$02,PBDR ULANG #$03,PBDR ULANG START
INIT
ULANG
Example 8
Example
The M68230 is connected to the circuit shown. Write a program to display 10 on the 7-segment when the switch is activated. Else, display 55 when the switch is not activated.
Flowchart
Program
START PGCR PACR PBCR PADDR PBDDR PADR PBDR ORG EQU EQU EQU EQU EQU EQU EQU $1000 $800001 $80000D $80000F $800005 $800007 $800011 $800013 READSW MOVE.B AND.B CMP.B BEQ CMP.B BEQ BRA INIT MOVE.B MOVE.B MOVE.B MOVE.B MOVE.B #$00,PGCR #$80,PACR #$80,PBCR #$00,PADDR #$FF,PBDDR DISP55 MOVE.B BRA PADR,D0 #$01,D0 #$00,D0 DISP55 #$01,D0 DISP10 READSW #$55,PBDR READSW
DISP10
MOVE.B BRA
END
#$10,PBDR READSW
START
The End