You are on page 1of 112

Rotate and Shift

Rotate and Shift


Contains instructions to rotate and shift bits around. Logical Shift Left/Right (LSL/LSR):

Shifts

bits to left or right by inserting zeros.

LSL (Logical Shift Left)

LSR (Logical Shift Right)

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

0000 LSR D0.W = 0

0000 0000 1010 1010 0 0 A A


Pushed out

D0.W =

ROL & ROR (Rotate Left/Right)


Pushes out MSB, and moves the bits to the back. C set to last bit pushed out.

ROL (Rotate Left)

ROR (Rotate Right)

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

And sent to front

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>.

Unconditional Branching Example


Instruction #1 Instruction #2 BRA HERE Instruction #3 Instruction #4 HERE Instruction #5 Instruction #6
Never executed.

Infinite Loop Example


REPEAT Instructions #1 Instructions #2 Instructions #n BRA REPEAT

Compare and Branch

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.

To test the condition, the CMP (Compare) instruction is used.

Bcc Flowchart
START

Evaluate condition using CMP

TRUE

Condition = TRUE?

FALSE

Go to LABEL

Execute next line

FINISH

Conditions (cc)
CMP.B D1,D0

BGT BGE Bcc BEQ BNE BLE BLT

D0.B > D1.B D0.B D1.B D0.B = D1.B D0.B D1.B D0.B D1.B D0.B < D1.B

BRA: Branch Unconditionally.

*(Antonakos, pg. 84)

Bcc Format
LABEL Instructions #1 Instructions #2 Instructions #n Test Condition (CMP) Bcc LABEL
If conditions = FALSE

If condition = TRUE

Instructions after Bcc

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

IfElse is tests a condition, and determines whether it is true or not.


If

the condition is true, execute a block of statements. Else, execute another block of statements.

Example: IfElse Using Bcc

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

Example: IfElse Using Bcc

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.

Basic Shapes Terminator


Indicates beginning and end of flowchart. Once at beginning, once at end. Examples:

START FINISH

Basic Shapes - Process


Describes actions to be done. Represented as rectangles.

Short

description of process in rectangle.

Example:

A=A+B

Reset Ports

Clear D0

Basic Shapes - Decision


Shows alternative program flow based on condition. Represented as diamond shape. Should have 2 arrows, representing TRUE and FALSE program flows. Can be used in ifelse, while, and for situations.

Basic Shapes - Decision

Examples:
Port is active?
TRUE

D0 = 3?

FALSE

TRUE

FALSE

Basic Shapes Input/Output


Shows the process of inputting or outputting data. Represented using rhombus. Examples:

Input D0 Show calculation results

Basic Shapes - Connectors


Used to link large process flows together. Represented using circles, with numbers inside. Numbers indicate connection. Examples:

1 3

Example: Connector
START 1

Input D0 D0 = D0 x D1

Input D1
FINISH 1

Writing the Program


Once flowchart is complete, write code to implement program. Follow program flow closely. Check and fix problems if necessary.

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.

Flowchart & Program


START START ORG $1000 ENTER DATA INTO A & B MOVE.B #3,$5000 MOVE.B #4,$6000

TRUE
A > B?

FALSE

STORE A AT $7000

STORE B AT $7000

SIMPANA SIMPANB

MOVE.B MOVE.B CMP.B BGT BLE MOVE.B BRA MOVE.B

$5000,D0 $6000,D1 D1,D0 SIMPANA SIMPANB D0,$7000 HABIS D1,$7000

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

Input numbers into memory locations. Load starting address into A0

D0 = 0?
FALSE

D1 = D1 + (A0) Go to next memory location

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

Input numbers into memory locations.

Load starting address into A0

MOVE.L

#$001100,A0

D0 = 10

MOVE.B

#10,D0

Clear D1
D0 = 0?
FALSE

CLR.B

D1

CMP.B #$0,D0, LOOPBNE LOOP

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 can also be used to control devices such as:


Switches. 7-Segment. LED

(Light Emitting Diodes). DC Motors.

M68k cannot control the devices directly, it has to use the M68230 I/O chip to control the devices.

M68230 Interfacing
Memory

M68k (LED, Switches, Motor, 7-Segment, Keypad, etc.)


CS*

Data Bus M68230

Port A Port B Port C

Device #1 Device #2 Device #3

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).

Port Addresses: Example


PI/T ADDRESSABLE REGISTER Port General Control Register Port A Data Direction Register Port B Data Direction Register ABBREVIATION PGCR PADDR PBDDR ADDRESS $800001 $800005 $800007

Port C Data Direction Register


Port A Control Register Port B Control Register Port A Data Register

PCDDR
PACR PBCR PADR

$800009
$80000D $80000F $800011

Port B Data Register


Port C Data Register

PBDR
PCDR

$800013
$800019

Initializing M68230 Registers

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

$800001 $800005 $800007 $800009 $80000D $80000F $800011 $800013 $800019


#$00,PGCR #$80,PACR #$80,PBCR

Setting the DDR

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

Example LED & Switches (PADDR = $00, PBDDR = $FF)


+5V
R

M68230 PA0 PB0 PA1 PB1 PB2 PB3 PB4

LED0 LED1 LED2

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

Example: 7-Segment + BCD Decoder


M68230
a B0 b B1 c

B2
B3 B4 B5 B6 B7

BCD 7-Seg Decoder

d e f g

MOVE.B

#$0F,PBDDR

Port X Data Registers (PXDR)


Can either be PADR, PBDR or PCDR. Contains the data sent/received by the hardware.

Example: Turn On LEDs


M68230 PB0 PB1 If 1 is sent to the LED, the LED will be forward biased and turn on. To turn on the LED according to the figure, the instruction: MOVE.B PB4 PB5 PB6 PB7 is executed. #$55,PBDR

PB2
PB3

Example: Read Data from Switches (MOVE.B PADR,D0)


+5V
R

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

Example: Display 4 on 7-Segment


M68230
a B0 b B1 c B2 B3 B4 B5 B6 B7 d e f g

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 BCD to 7 Segment Decoder

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.

7-Segment + BCD Decoder


M68230
a B0 b B1 c

B2
B3 B4 B5 B6 B7

BCD 7-Seg Decoder

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

BCD 7-Seg Decoder

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

Example 1: Set LED


A set of LEDs are connected to Port B in M68230. Write a program that turns on LED3 and LED4, and turns off the rest.
Port PGCR PACR Address $800001 $800005

M68230 PB0 PB1

LED0 LED1 LED2 LED3 LED4 LED5

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

ORG EQU EQU EQU EQU


MOVE.B MOVE.B MOVE.B MOVE.B END

$1000 $800001 $800007 $80000D $800013


#$00,PGCR #$80,PBCR #$FF,PBDDR #%00011000,PBDR START

ONLED

Example 1: Set LED


M68230 PB0 PB1 0 0 0 1 1 0 0 0 LED0 LED1 LED2 LED3 LED4 LED5 LED6 LED7

PB2
PB3 PB4 PB5 PB6 PB7

Example 2

Example 2: Read Switches & Output to LED

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

LED0 LED1 LED2

PA1
R

PB1 PB2 PB3 PB4

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

Solution Complete Program


START PGCR PACR PBCR PADDR PBDDR PADR PADR ORG EQU EQU EQU EQU EQU EQU EQU $1000 $A00001 $A0000D $A0000F $A00005 $A00007 $A00011 $A00013 INIT MOVE.B MOVE.B MOVE.B MOVE.B MOVE.B #$00,PGCR #$80,PACR #$80,PBCR #$00,PADDR #$FF,PBDDR

LOOP

MOVE.B PADR,D0 MOVE.B D0,PBDR BRA LOOP END START

Sample Output
+5V M68230 PA0 PB0
R

LED0 LED1 LED2

PA1
R

PB1 PB2 PB3 PB4

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

MOVE.B MOVE.B MOVE.B MOVE.B MOVE.B

#$00,PGCR #$80,PACR #$80,PBCR #$00,PADDR #$FF,PBDDR

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

Based on the circuit given, display the number 3 on the 7-segment.

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

Example: Displaying C on 7-Seg


M68230
a=1 B0 B1 B2 B3 B4 B5 B6 B7 d=1 e=1 f=1 g=0 b=0 c=0

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

MOVE.B MOVE.B MOVE.B


MOVE.B END

#$00,PGCR #$80,PBCR #$FF,PBDDR


#$B9,PBDR START

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

MOVE.B BRA OFF7SEG MOVE.B HABIS

INIT

MOVE.B MOVE.B MOVE.B MOVE.B MOVE.B

#$00,PGCR #$80,PACR #$80,PBCR #$00,PADDR #$F1,PBDDR

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

MOVE.B MOVE.B MOVE.B MOVE.B MOVE.B MOVE.B AND.B

#$00,PGCR #$80,PACR #$80,PBCR #$00,PADDR #$0F,PBDDR PADR,D0 #$03,D0

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

You might also like