0% found this document useful (0 votes)
140 views126 pages

SH66XX Programming Guide V10

Instructions for data transmission, arithmetic operation, Logic Operation and Flow Control. 2-1 Data Transmission. 33 2-2 4 / 8-Bit Addition. 34 2-3 4-Bit Subtraction. 36 2-4 4 /-8-Bit Multiplication. 38 2-5 4-bit Division. 38 2-8 8-bit Up / Down Counter. 42 2-9 8-bit Right Movement of n bit(s) (n=13 / 7). 48 2-8 8

Uploaded by

tying2005
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
140 views126 pages

SH66XX Programming Guide V10

Instructions for data transmission, arithmetic operation, Logic Operation and Flow Control. 2-1 Data Transmission. 33 2-2 4 / 8-Bit Addition. 34 2-3 4-Bit Subtraction. 36 2-4 4 /-8-Bit Multiplication. 38 2-5 4-bit Division. 38 2-8 8-bit Up / Down Counter. 42 2-9 8-bit Right Movement of n bit(s) (n=13 / 7). 48 2-8 8

Uploaded by

tying2005
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 126

CONTENTS

Chapter One .................................................................................................. 7


Analysis of SH6610 Instructions ................................................................. 7
1-1 Instruction Categories................................................................................................. 8
1-2 Explanation of Symbols .............................................................................................. 8
1-3 Instructions for Data Transmission............................................................................. 9
1-4 Instructions for Arithmetic Operation ........................................................................ 11
1-5 Instructions for Logic Operation................................................................................ 20
1-6 Instructions for Flow Control..................................................................................... 25

CHAPTER TWO ............................................................................................33


System Software Application......................................................................33
2-1 Data Transmission ................................................................................................. 33
2-2 4/8-Bit Addition....................................................................................................... 34
2-3 4/8-Bit Subtraction.................................................................................................. 36
2-4 4/8-Bit Multiplication ............................................................................................... 38
2-5 4/8-Bit Division ....................................................................................................... 40
2-6 4/8-Bit Left Movement of n bit(s) (n=1~3/7) ........................................................... 42
2-7 4/8-Bit Right Movement of n bit(s) (n=1~3/7)......................................................... 44
2-8 8-Bit Up/Down Counter .......................................................................................... 48
2-9 Conversion Between Decimal and Binary ............................................................. 50
2-10 Square Operation................................................................................................. 54
2-11 Conversion Between ASCII And Hex .................................................................. 56
2-12 Conversion Between ASCII And Decimal............................................................ 60
2-13 Conversion From Decimal To 7 Segment Display............................................... 63
2-14 BCD 4/8-Bit Addition ............................................................................................ 65
2-15 BCD 4/8-Bit Subtraction....................................................................................... 68
2-16 BCD 4/8-Bit Multiplication .................................................................................... 71
2-17 BCD 4/8-Bit Division ............................................................................................ 74
2-18 Compare Routine ................................................................................................. 77
2-19 Delay .................................................................................................................... 78
2-20 Loop ..................................................................................................................... 79

2-21 Random Number Generator ................................................................................ 81


2-22 Check Sum of Data .............................................................................................. 83

CHAPTER THREE ........................................................................................85


System Hardware Application ....................................................................85
3-1 Timer......................................................................................................................... 85
3-1.1 Timer 0 .............................................................................................................. 86
3-1.2 Timer 1 .............................................................................................................. 87
3-2 I/O Port Control......................................................................................................... 87
3-3 Interrupt Control........................................................................................................ 89
3-3.1 External Interrupt (INT *) ................................................................................... 90
3-3.2 Timer Interrupt................................................................................................... 90
3-3.3 Port Interrupt ..................................................................................................... 91
3-3.4 Interrupt Priority................................................................................................. 91
3-4 Keyboard Control...................................................................................................... 91
3-4.1 Theory of Keyboard........................................................................................... 91
3-4.2 Single button switch key.................................................................................... 92
3-4.3 Continual and non-continual key presses ......................................................... 95
3-4.4 4x4 Matrix Keyboard ......................................................................................... 97
3-5 LED Control ............................................................................................................ 101
3-5.1 Single LED display .......................................................................................... 101
3-5.2 Seven-Segment LED Display.......................................................................... 102
3-5.3 LED Decimal display(0~9)............................................................................... 105
3-6 Analog to Digital Conversion Control ..................................................................... 108
3-6.1 Introduction of ADC-0801................................................................................ 108
3-6.2.1 ADC0801 Function Experiment.................................................................... 110
3-7 PWM Control Application........................................................................................ 113
3-7.1 D/A using PWM ............................................................................................... 113
3-7.2 Sine Wave Producer ....................................................................................... 116
3-8 Music Control (PSG)............................................................................................... 119
3-8.1 Theory of PSG................................................................................................. 119
3-8.2 Music Player Program ..................................................................................... 120

2/126

Ver1.0

INDEX OF EXAMPLES AND INSTRUCTIONS


[Chart 3-2.1] CMOS Type.................................................................................................................. 88
[Chart 3-2.2] Bidirection Type ........................................................................................................... 88
[Chart 3-4.2.1] Single button switch key.......................................................................................... 92
[Chart 3-5.1.1] LED Driving Circuit................................................................................................. 101
[Chart 3-5.2.1] Common-cathode seven-segment display ......................................................... 102
[Chart 3-5.3.1] 0~9 Counter circuit................................................................................................. 105
[Chart 3-6.1.1] Chart of ADC0801 pins ......................................................................................... 108
[Chart 3-6.2.1] ADC0801 application circuit chart........................................................................ 110
[Chart 3-7.1.1] DAC circuit chart .................................................................................................... 113
[Chart 3-7.2.1] Sine Wave Producer circuit .................................................................................. 116
[Chart3-4.4.1] 4x4 Keyboard Structure Chart................................................................................. 97
[Chart3-5.2.2] Common-anode seven-segment display ............................................................. 104
[Chart3-8.1.1] PSG block chart....................................................................................................... 119
[Example 2-6.1] 4-digit left move by n bit (s) (n=1~3) ................................................................... 42
[Example2-1.1] Data transfer ............................................................................................................ 33
[Example2-10.1] Looking up in table to get the squares (for 0-9) ............................................... 54
[Example2-11.1] ASCII Hex .......................................................................................................... 56
[Example2-11.2] Hex ASCII ......................................................................................................... 58
[Example2-12.1] ASCII Decimal ................................................................................................... 60
[Example2-12.2] Decimal ASCII .................................................................................................. 62
[Example2-13.1] Decimal Seven segment.................................................................................. 63
[Example2-14.1]BCD 4 Digit Addition.............................................................................................. 65
[Example2-14.2] BCD 8 Digit Addition............................................................................................. 66
[Example2-15.1] BCD 4-digit subtraction ........................................................................................ 68
[Example2-15.2] BCD 8-digit subtraction ........................................................................................ 69
[Example2-16.1] BCD 4-digit multiplication .................................................................................... 71
[Example2-16.2]BCD 8-digit multiplication...................................................................................... 72
[Example2-17.1] BCD 4-digit division .............................................................................................. 74
[Example2-17.2] BCD 8-digit division .............................................................................................. 75
[Example2-18.1] Compare routine ................................................................................................... 77
[Example2-19.1] Calling delay subprogram.................................................................................... 78
[Example2-2.1] 4-digit addition (5h+4h) .......................................................................................... 34
[Example2-2.2] 8-digit addition (1Fh+44h)...................................................................................... 35
[Example2-20.1] LOOP subprogram ............................................................................................... 79
[Example2-21.1] Generating four random numbers ...................................................................... 81
3/126

Ver1.0

[Example2-22.1] Check sum of data................................................................................................ 83


[Example2-3.1] 4-digit subtraction (5H-4H) .................................................................................... 36
[Example2-3.2] 8-digit subtraction (44H-1FH)................................................................................ 37
[Example2-4.1] 4-digit multiplication ................................................................................................ 38
[Example2-4.2] 8-digit multiplication ................................................................................................ 39
[Example2-5.1] 4-digit division.......................................................................................................... 40
[Example2-5.2] 8-digit division.......................................................................................................... 40
[Example2-6.2] 8-digit left move by n bit (s) (n=1~7) .................................................................... 43
[Example2-7.1] 4-digit right move by n bit(s) (n=1~3) ................................................................... 44
[Example2-7.2] 8-digit right move by n bit(s) (n=1~7) ................................................................... 46
[Example2-8.1] 8-digit up counter .................................................................................................... 48
[Example2-8.2] 8-digit down counter ............................................................................................... 49
[Example2-9.1] BinaryBCD ........................................................................................................... 50
[Example2-9.2] BCD Binary .......................................................................................................... 52
[Example3-1.1] Setting up timer 0 .................................................................................................... 86
[Example3-1.2] Setting up timer 1 .................................................................................................... 87
[Example3-4.2.1] Single button switch key ..................................................................................... 93
[Example3-4.3.1] Continual key press............................................................................................. 95
[Example3-4.4.1] 4X4 Matrix Keyboard Scan ................................................................................ 97
[Example3-5.3.1] 0~9 Counter........................................................................................................ 105
[Example3-6.2.1] ADC0801 application program ........................................................................ 111
[Example3-7.1.1] D/A using PWM.................................................................................................. 114
[Example3-7.2.1] Sine Wave Producer ......................................................................................... 117
[Example3-8.2.1] Playing a piece of music................................................................................... 122
[Example3-8.2.2] Simulating the sound of a train ........................................................................ 124
[Table 3-1.1] Timer controlled buffer................................................................................................ 85
[Table 3-8.2.1] NT6510 musical scale table ................................................................................. 121
[Table3-1.1.1] Timer0 frequency table............................................................................................. 86
[Table3-1.2.1] Timer1 frequency table............................................................................................. 87
[Table3-2.1] I/O port address ............................................................................................................ 87
[Table3-3.1] Table of interrupt control digits ................................................................................... 89
[Table3-5.2.1] Character code for common-cathode seven-segment display......................... 103
[Table3-5.2.2] Character code for common-anode seven-segment display............................ 104
[Table3-8.1.1] PSG VOL.................................................................................................................. 119
[Table3-8.1.2] Table of PSG control digits .................................................................................... 120
[Table3-8.1.3] Prescaler table......................................................................................................... 120
ADC ...................................................................................................................................................... 12
4/126

Ver1.0

ADCM ................................................................................................................................................... 13
ADD ...................................................................................................................................................... 11
ADDM ................................................................................................................................................... 11
ADI ........................................................................................................................................................ 13
ADIM ..................................................................................................................................................... 14
AND ...................................................................................................................................................... 20
ANDIM .................................................................................................................................................. 21
ANDM ................................................................................................................................................... 20
BA0 ....................................................................................................................................................... 26
BA1 ....................................................................................................................................................... 27
BA2 ....................................................................................................................................................... 27
BA3 ....................................................................................................................................................... 28
BC ......................................................................................................................................................... 28
CALL..................................................................................................................................................... 29
DAA....................................................................................................................................................... 14
DAS....................................................................................................................................................... 19
EOR ...................................................................................................................................................... 23
EORIM.................................................................................................................................................. 24
EORM................................................................................................................................................... 24
HALT..................................................................................................................................................... 31
JMP....................................................................................................................................................... 25
LDA ....................................................................................................................................................... 10
LDI........................................................................................................................................................... 9
NOP ...................................................................................................................................................... 32
OR......................................................................................................................................................... 22
ORIM .................................................................................................................................................... 23
ORM...................................................................................................................................................... 22
RTNI...................................................................................................................................................... 30
RTNW................................................................................................................................................... 29
SBC....................................................................................................................................................... 17
SBCM ................................................................................................................................................... 17
SBI ........................................................................................................................................................ 18
SBIM ..................................................................................................................................................... 19
STA ....................................................................................................................................................... 10
STOP .................................................................................................................................................... 31
SUB....................................................................................................................................................... 15
SUBM ................................................................................................................................................... 16
5/126

Ver1.0

TJMP .................................................................................................................................................... 29

6/126

Ver1.0

Chapter One
Analysis of SH6610 Instructions
The following are SH6610 instructions, categorized and explained according to their
respective functions. When you need an instruction for a certain function, you can look up
the instruction in its function category. Youd better browse through all the instructions listed
here, because even though you cant remember all of them at once, you can still have an
impression to remind you of such instruction when needed. Some people dont even know
about some very good instructions that he can use, just because he didnt browse through all
of them. This ignorance may cause considerable waste of space on a system without a big
enough ROM, which is so regrettable. I will explain as simply as possible to improve your
learning efficiency. Of course, it is normal to understand or remember only part of the
contents after reading once. And you are certain often revert to this book because it is a
collection of instructions.

Instructions
Instruction is a series of codes that can be recognized by CPU, and then CPU will
operate according to the given instruction.
Operand
Apart from instructions to tell CPU how to operate, the operation object must be
designated. The object for CPU to operate with is called Operand. Therefore, a complete
instruction must include two items, both instruction and operand.

Format of SH6610 Instructions


Instruction

[Operand 1], [Operand 2]

Of the above, items inside [ ] are used according to nature of the instruction. Some
instructions require only one operand, while some require two. Instruction and operand shall
be separated by a space, and operands shall be separated from each other by a ,.
Execution Time of Instruction
Execution time for SH6610 instructions is one instruction cycle, which is one fourth of the
system working frequency.

7/126

Ver1.0

1-1 Instruction Categories


According to their functions, SH6610 instructions can be classified into four categories:
z

Arithmetic Operation Instructions


I.
addition: ADCADCMADDADDMADIADIM
II.
Subtraction: SBCSBCMSUBSUBMSBISBIM
III.
BCD: DAADAS

Logic Operation Instructions


EOREORMEORIMORORMORIMANDANDMANDIM

Data Transmission Instructions


LDASTALDI

Flow Control Instructions


BAZBCBA0BA1BA2BA3CALLRTNWRTNIHALTSTOPJMPTJMP

The above are all of the instructions of SH6610 series, which add up to only 40 in
number, but never overlook them! A variety of consumer electrical products on the market
are created with them, e.g. calculator, remote controller, watch, toy, etc.

1-2 Explanation of Symbols


Before going to our subject, we list the symbols that may appear afterwards so that our
readers can understand this book more easily. The symbols are listed as follows:
PC
AC
CY
Mx
bbb
ST
TBR
X
I
&
|
^

Program Counter
Accumulator
Carry Flag
Data Memory
RAM bank
Stack
Table Branch Register
Program Address
Immediate Data
Logic AND
Logic OR
Logic EOR

Now lets enter the world of instructions of SH6610 series. Let s go

8/126

Ver1.0

1-3 Instructions for Data Transmission


During the internal operation of the system, data is transmitted rapidly and incessantly
between memories or registers. This fast and incessant transmission is the power of
system capability. SH6610 system provides several instructions for data transmission, as
follows:
Instruction: LDI
Function: to load Immediate Data I to Accumulator and Data
Memory
Format:
Instruction Code:
Carry Flag:
Operation:

LDI MxI
01111 iiii xxx xxxx
Not affected
AC , Mx I

Explanation
LDI is a very frequently used instruction. It loads immediate data I to accumulator and
data memory. However, due to this 4-bit system, the preset range of the immediate data I is
00H ~ 0FH(0 ~ 15), and that of Mx is 00H~7FH.
[ Example ]
LDI
20H05H
after execution:
AC=05H
Content of data memory $20H=05H

Programming Tip
There isnt any specially defined register for users in SH6610 system, but we can use its
powerful data memory as registers in our program designing. In the above example, I have
used data memory $20H for a register. However, if they are expressed only by address and
without respective names, the design of the program will be very confusing. Heres a tip for
you: you can use the pseudo-instruction EQU to define each data address.
AAA
Name of memory variable

EQU

20H

PseudoInstruction

Data memory address


($00H~$7FH)

Therefore, the LDI instruction can also be written like this:


LDI

AAA05H

9/126

Ver1.0

Instruction: STA
Format:
Instruction Code:
Carry Flag:
Operation:

Function:to store the value of Accumulator to Data Memory


STA Mxbbb
00111 1bbb xxx xxxx
Not affected
Mx AC

Explanation
STA loads the value of accumulator to data memory. When executing this instruction,
CPU does the transmission only and the carry flag is not affected.
[ Example ] Save the value of AC in $21H

LDI
STA

:
20H05H
21H00H
:

;AC=05H$20H=05H
;$21H=05H

Programming Tip
If operand 2 is the immediate data I, then the program can be written in the following
ways:
LDI
LDI
LDI

20H,0AH
20H,10
20H,1010B

Instruction: LDA
Format:
Instruction Code:
Carry Flag:
Operation:

;expressed in hex
;expressed in decimal
;expressed in binary

Function: to load the value of Data Memory to Accumulator


LDA Mxbbb
00111 0bbb xxx xxxx
Not affected
AC Mx

Explanation
LDA loads the value of data memory to accumulator. This instruction does not affect
the carry flag.
[ Example ] Load the value of $20H to AC
:
LDI
20H,05H
;$20H=05H,AC=05H
LDI
21H,0FH
;$21H=0FH,AC=0FH
LDA
20H,0
;AC=05H
:

Programming Tip
When writing a program, we often use labels as jumping destinations in the program.
Label names can be defined by user according to the following rules:
I.
II.

A label mustnt begin with number or space.


Length of a label mustnt exceed 7 characters. Only the first 7 characters will be
recognized for labels exceeding that length.

10/126

Ver1.0

1-4 Instructions for Arithmetic Operation


SH6610 series provide some frequently used arithmetic (integer) operation instructions,
like: Addition, subtraction, BCD adjustment, etc.
Instruction: ADD
Function: to add up the values of Data Memory and Accumulator,
and then save the result in the Accumulator
Format:
ADD Mxbbb
Instruction Code:
00001 0bbb xxx xxxx
Carry Flag:
CY
Operation:
AC Mx + AC
Explanation
Instruction ADD adds up the values of data memory and accumulator and saves the
result in the accumulator. The ADD operation affects carry flag: when the result of ADD
exceeds 0FH, the carry flag is set to 1; otherwise the value of CY is 0. Therefore we can
decide whether theres a carry by the value of the carry flag after addition.

[ Example ] 05H +06H


:
LDI
20H,05H
LDI
21H,06H
LDA
20H,00H
ADD
21H,0
[ Example ] 0BH + 06H
:
LDI
20H,0BH
LDI
21H,06H
LDA
20H,00H
ADD
21H,0
:
Instruction: ADDM

Format:
Instruction Code:
Carry Flag:
Operation:

;$20H=05H,AC=05H
;$21H=06H,AC=06H
;AC=05H
;AC=0BH,CY=0,$21H=06H
:

;$20H=0BH,AC=0BH
;$21H=06H,AC=06H
;AC=0BH,CY=0
;AC=01H,CY=1,$21H=06H

Function: to add up the values of Data Memory and Accumulator,


and save the result in both the Accumulator and the Data
Memory
ADDM Mxbbb
00001 1bbb xxx xxxx
CY
AC , Mx Mx + AC

Explanation
Instruction ADDM adds up the values of data memory and accumulator and saves the
result in both the accumulator and the data memory. The ADDM operation affects carry flag:
when the result of ADDM exceeds 0FH, the carry flag is set to 1; otherwise the value CY is 0.
Therefore we can decide whether theres a carry by the value of the carry flag after addition.
[ Example ] 05H +06H
:
LDI
20H,05H
LDI
21H,06H
LDA
20H,00H
ADDM 21H,0

;$20H=05H,AC=05H
;$21H=06H,AC=06H
;AC=05H
;AC=0BH,$21H=0BH,CY=0
11/126

Ver1.0

:
[ Example ] 0BH + 06H
:
LDI
20H,0BH
LDI
21H,06H
LDA
20H,00H
ADDM 21H,0
:

;$20H=0BH,AC=0BH
;$21H=06H,AC=06H
;AC=0BH,CY=0
;AC=01H,$21H=01H,CY=1

Programming Tip
When you are reading the examples, I suggest that you call them into ICE after compiling
to watch the change in each of the registers step by step.
Instruction: ADC

Format:
Instruction Code:
Carry Flag:
Operation:

Function: to add up the value of Data Memory, Carry Flay and the
value of Accumulator, and then save the result in the
Accumulator
ADC Mxbbb
00000 0bbb xxx xxxx
CY
AC Mx + AC + CY

Explanation
Instruction ADC adds up the value of data memory, carry flag and the value of
accumulator, and saves the result in the accumulator. The ADC operation affects the carry
flag: when the result of ADC exceeds 0FH, the carry flag is set to 1; otherwise the value of
CY is 0. Therefore we can decide whether theres a carry by the value of the carry flag after
addition.
[ Example ] 05H +06H , CY=1
:
LDI
20H,05H
LDI
21H,06H
LDA
20H,00H
ADC
21H,0
:

;CY=1
;$20H=05H,AC=05H
;$21H=06H,AC=06H
;AC=05H
;AC=0CH,$21H=06H,CY=0

[ Example ] 0BH + 06H , CY=0


:
LDI
20H,0BH
LDI
21H,06H
LDA
20H,00H
ADC
21H,0
:

;CY=0
;$20H=0BH,AC=0BH
;$21H=06H,AC=06H
;AC=0BH
;AC=01H,$21H=06H,CY=1

Programming Tip
When carry is not considered, youd better use ADD rather than ADC, in order to avoid
extra uncertainty due to the addition of CY value (because CY can be either 1 or 0).

12/126

Ver1.0

Instruction: ADCM

Format:
Instruction Code:
Carry Flag:
Operation:

Function: to add up the value of Data Memory, Carry Flag and the
value of Accumulator, and then save the result in both
the Accumulator and the Data Memory
ADCM Mxbbb
00000 1bbb xxx xxxx
CY
AC , Mx Mx + AC + CY

Explanation
Instruction ADCM adds up the value of data memory, carry flag and the value of
accumulator, and saves the result in both the accumulator and the data memory. The
ADCM operation affects the carry flag: when the result of ADCM exceeds 0FH, the carry flag
is set to1; otherwise the value of CY is 0. Therefore we can decide whether theres a carry
by the value of the carry flag after addition.
[ Example ] 05H +06H , CY=1
:
;CY=1
LDI
20H,05H
;$20H=05H,AC=05H
LDI
21H,06H
;$21H=06H,AC=06H
LDA
20H,00H
;AC=05H
ADCM
21H,0
;AC=0CH,$21H=0CH,CY=0
:
[ Example ] 0BH + 06H , CY=0
LDI
LDI
LDA
ADCM

Instruction: ADI
Format:
Instruction Code:
Carry Flag:
Operation:

:
;CY=0
20H,0BH ;$20H=0BH,AC=0BH
21H,06H
;$21H=06H,AC=06H
20H,00H
;AC=0BH,
21H,0
;AC=01H,$21H=01H,CY=1
:

Function: to add up the value of Data Memory and Immediate Data


I, and then save the result in Accumulator
ADI MxI
01000 iiii xxx xxxx
CY
AC Mx + I

Explanation
Instruction ADI adds up the value of data memory and immediate data I, and saves the
result in accumulator. The ADI operation affects carry flag: when the result of ADI exceeds
0FH, the carry flag is set to1; otherwise the value of CY is 0. Therefore we can decide
whether theres a carry by the value of the carry flag after addition.
[ Example ] $20H=05H , I=04H
:
LDI
20H,05H
ADI
20H,04H
:
[ Example ] $20H=0AH , I=07H
:
LDI
20H,0AH
ADI
20H,07H
:

;$20H=05H,AC=05H,CY=0
;AC=09H,$20H=05H,CY=0

;$20H=0AH,AC=0AH,CY=0
;AC=01H,$20H=0AH,CY=1

13/126

Ver1.0

Instruction: ADIM

Format:
Instruction Code:
Carry Flag:
Operation:

Function: to add up the value of Data Memory and Immediate Data


I, and then save the result in both Accumulator and the
Data Memory
ADIM MxI
01001 iiii xxx xxxx
CY
AC , Mx Mx + I

Explanation
Instruction ADIM adds up the value of data memory and immediate data I, and saves the
result in both accumulator and the data memory. The ADIM operation affects carry flag:
when the result of ADIM exceeds 0FH, the carry flag is set to1; otherwise the value of CY is 0.
Therefore we can decide by the value of the carry flag after addition whether there is a carry.
[ Example ] $20H=05H , I=04H
:
LDI
ADIM
:
[ Example ] $20H=0AH , I=07H
:
LDI
ADIM
:
Instruction: DAA

Format:
Instruction Code:
Carry Flag:
Operation:

20H,05H
20H,04H

;$20H=05H,AC=05H
;AC=09H,$20H=09H,CY=0

20H,0AH
20H,07H

;$20H=0AH,AC=0AH
;AC=01H,$20H=01H,CY=1

Function: to adjust the value of Data Memory to decimal after


addition, and then save the result in both Accumulator
and the Data Memory
DAA Mx
11001 0110 xxx xxxx
CY
AC ; Mx Decimal , adjust AC for Add

Explanation
Instruction DAA acts by adjusting the value of data memory to decimal after addition and
saving the result to both accumulator and the data memory. Its adjusting method is if the
value of the data memory is greater than 9 or if CY= 1, then add 6 to the data memory and
set the carry flag to 1.
[ Example ] 06H + 05H , and do DAA adjustment
:
LDI
20H,06H
LDI
21H,05H
LDA
20H,0
ADD
21H,0
DAA
21H
:

14/126

;AC=06H,$20H=06H
;AC=05H,$21H=05H
;AC=06H
;AC=0BH,CY=0
;AC=01H,$21H=01H,CY=1

Ver1.0

Instruction: SUB

Format:
Instruction Code:
Carry Flag:
Operation:

Function: to subtract the value of Accumulator from the value of


Data Memory, and then save the result in the
Accumulator
SUB Mxbbb
00011 0bbb xxx xxxx
CY
AC Mx - AC

Explanation
SUB subtracts the value of accumulator from the value of data memory and saves the
result in the accumulator. When executing SUB, if the value of data memory is less than the
value of accumulator, a borrow will take place and CY will be set to 0. On the contrary, if
the value of data memory is greater than the value of accumulator, borrow will not happen
and CY will be set to 1. Therefore we can decide by the value of CY whether there is a
borrow after execution of SUB. Besides, subtraction in the system is done through addition,
i.e. when subtracting a number, it is actually adding the numbers binary complement.
[ Example ] 06H - 05H
:
LDI
20H,05H
LDI
21H,06H
LDA
20H,0
SUB
21H,0
:
[ Example ] 05H - 06H
:
LDI
20H,05H
LDI
21H,06H
LDA
21H,0
SUB
20H,0
:

;AC=05H,$20H=05H
;AC=06H,$21H=06H
;AC=05H
;AC=1,CY=1,$21H=06H

;AC=05H,$20H=05H
;AC=06H,$21H=06H
;AC=06H
;AC=0FH,CY=0,$20H=05H

15/126

Ver1.0

Instruction: SUBM

Format:
Instruction Code:
Carry Flag:
Operation:

Function: to subtract the value of Accumulator from the value of


Data Memory, and then save the result in both the
Accumulator and the Data Memory
SUBM Mxbbb
00011 1bbb xxx xxxx
CY
AC , Mx Mx - AC

Explanation
System movement of SUBM is almost the same as SUB, it subtracts current value of
accumulator from the value of data memory and saves the result in the accumulator as well
as in the data memory. When executing SUBM, if the value of data memory is less than the
value of accumulator, a borrow will take place and CY will be set to 0. On the contrary, if
the value of data memory is greater than the value of accumulator, borrow will not happen
and CY will be 1. Therefore we can decide by the value of CY whether there is a borrow
after execution of SUBM.
[ Example ] 06H - 05H
:
LDI
20H,05H
LDI
21H,06H
LDA
20H,0
SUBM 21H,0
:
[ Example ] 05H - 06H
:
LDI
20H,05H
LDI
21H,06H
LDA
21H,0
SUBM 20H,0
:

;AC=05H,$20H=05H
;AC=06H,$21H=06H
;AC=05H
;AC=01H,CY=1,$21H=01H

;AC=05H,$20H=05H
;AC=06H,$21H=06H
;AC=06H
;AC=0FH,CY=0,$20H=0FH

16/126

Ver1.0

Instruction: SBC

Format:
Instruction Code:
Carry Flag:
Operation:

Function: to subtract the value of Accumulator from the value of


Data Memory, add Carry Flag, and then save the result in
the Accumulator
SBC Mxbbb
00010 0bbb xxx xxxx
CY
AC Mx - AC + CY

Explanation
System movement of SBC is to subtract the value of accumulator from the value of
data memory, add the value of carry flag, and then save the result in the accumulator.
When executing SBC, if the value of data memory is less than the value of accumulator, a
borrow will take place and the CY will be set to 0. On the contrary, if the value of data
memory is greater than the value of accumulator, borrow will not happen and the CY will be 1.
Therefore we can decide by the value of CY whether there is a borrow after execution of
SBC.
[ Example ] CY=06 - 5=?
:
LDI
20H,05H
LDI
21H,06H
LDA
20H,0
SBC
21H,0
:

;CY=0
;AC=05H,$20H=05H
;AC=06H,$21H=06H
;AC=05H
;AC=01H,CY=0,$21H=06H

[ Example ] CY=16 - 5=?


:
LDI
20H,05H
LDI
21H,06H
LDA
20H,0
SBC
21H,0
:

;CY=1
;AC=05H,$20H=05H
;AC=06H,$21H=06H
;AC=05H
;AC=02H,CY=0,$21H=06H

Instruction: SBCM

Format:
Instruction Code:
Carry Flag:
Operation:

Function: to subtract the value of Accumulator from the value of


Data Memory, add Carry Flag, and then save the result in
both the Accumulator and the Data Memory
SBCM Mxbbb
00010 1bbb xxx xxxx
CY
AC , Mx Mx - AC + CY

Explanation
System movement of SBCM is to subtract the value of accumulator from the value of
data memory, add the value of carry flag, and then save the result in both the accumulator
and data memory. When executing SBCM, if the value of data memory is less than the
value of accumulator, a borrow will take place and the CY will be set to 0. On the contrary,
if the value of data memory is greater than the value of accumulator, borrow will not happen
and the CY will be set to 1. Therefore we can decide by the value of CY whether there is a
borrow after execution of SBCM.

17/126

Ver1.0

[ Example ] CY=06 - 5=?


:
LDI
20H,05H
LDI
21H,06H
LDA
20H,0
SBCM 21H,0
:

;CY=0
;AC=05H,$20H=05H
;AC=06H,$21H=06H
;AC=05H,CY=0
;AC=01H,CY=0,$21H=01H

[ Example ] CY=16 - 5=?


:
LDI
20H,05H
LDI
21H,06H
LDA
SBCM
:

;CY=1
;AC=05H,$20H=05H
;AC=06H,$21H=06H
20H,0
;AC=05H,CY=1
21H,0
;AC=02H,CY=0,$21H=02H

Instruction: SBI
Format:
Instruction Code:
Carry Flag:
Operation:

Function: to subtract the Immediate Data I from the value of Data


Memory, and then save the result in Accumulator
SBI MxI
01010 iiii xxx xxxx
CY
AC Mx - I

Explanation
System movement of SBI is to subtract immediate data I from the value of data
memory, and save the result in accumulator. When executing SBI, if the value of the data
memory is less than the immediate data, a borrow will take place and CY will be set to 0.
On the contrary, if the value of data memory is greater than the immediate data, borrow will
not happen and CY will be 1. Therefore we can decide by the value of CY whether there is
a borrow after execution of SBI.
[ Example ] $20H=05H , I=04H
:
LDI
20H,05H
SBI
20H,04H
:
[ Example ] $20H=02H , I=07H
:
LDI
20H,02H
SBI
20H,07H
:

;$20H=05H,AC=05H,CY=0
;AC=01H,$20H=05H,CY=1

;$20H=02H,AC=02H,CY=0
;AC=0BH,$20H=02H,CY=0

18/126

Ver1.0

Instruction: SBIM

Format:
Instruction Code:
Carry Flag:
Operation:

Function: to subtract Immediate Data I from the value of Data


Memory, and then save the result in both Accumulator
and the Data Memory
SBIM MxI
01011 iiii xxx xxxx
CY
AC , Mx Mx - I

Explanation
System movement of SBIM is to subtract immediate data I from the value of data
memory, and save the result in both accumulator and the data memory. When executing
SBIM, if the value of data memory is less than the immediate data, a borrow will take place
and CY will be set to 0. On the contrary, if the value of data memory is greater than the
immediate data, borrow will not happen and CY will be 1. Therefore we can decide by the
value of CY whether there is a borrow after execution of SBIM.
[ Example ] $20H=05H , I=04H
:
LDI
20H,05H
SBIM
20H,04H
:
[ Example ] $20H=02H , I=07H
:
LDI
20H,02H
SBIM
20H,07H
:
Instruction: DAS

Format:
Instruction Code:
Carry Flag:
Operation:

;$20H=05H,AC=05H
;AC=01H,$20H=01H,CY=1

;$20H=02H,AC=02H
;AC=0BH,$20H=0BH,CY=0

Function: to adjust the value of Data Memory to decimal after


subtraction, and save the result in Accumulator and the
Data Memory
DAS Mx
11001 1010 xxx xxxx
CY
AC ; Mx Decimal , adjust AC for Sub

Explanation
Instruction DAS acts by adjusting the value of data memory to decimal after
subtraction and saving the result to both accumulator and the data memory. Its adjusting
method is if the value of data memory is greater than 9 or if CY=0, then add 0AH to the data
memory and set CY to 0.

[ Example ] 05H - 06H , and do DAS adjustment


:
LDI
20H,06H
;AC=06H,$20H=06H
LDI
21H,05H
;AC=05H,$21H=05H
LDA
20H,0
;AC=06H
SUB
21H,0
;AC=0FH,CY=0
DAS
21H
;AC=09H,$21H=09H,CY=0
:

19/126

Ver1.0

1-5 Instructions for Logic Operation


Logic instructions are essential to system structure. SH6610 series MCU provide
some common logic instructions. Now Im going to explain to you one by one in most details,
and assist my explanation with simple examples, so that you can quickly understand action
theory of each instruction.
Instruction: AND

Format:
Instruction Code:
Carry Flag:
Operation:

Function: to do logic AND operation with the values of Data Memory


and Accumulator, and then save the result in the
Accumulator
AND Mxbbb
00110 1bbb xxx xxxx
Not affected
AC Mx & AC

Explanation
In AND operation, the result will be 1(true) only if both of the two operands are 1(true).
Its logic table is as follows:

Logic operation table for AND


a
0
0
1
1

b
0
1
0
1

AND
0
0
0
1

However, in real instruction the logic operand has 4 bits rather than 1 bit. The
instruction AND is to AND the values of data memory with accumulator, and the result is
saved in the accumulator.

[ Example ] 06H & 05H


:
LDI
LDI
AND
:
Instruction: ANDM

Format:
Instruction Code:
Carry Flag:
Operation:

20H,0110B
21H,0101B
20H,0

;AC=06H,$20H=06H
;AC=05H,$21H=05H
;AC=0100B,$20H=06H

Function: to do logic AND operation with the values of Data


Memory and Accumulator, and then save the result in
both the Accumulator and the Data Memory
ANDM Mxbbb
00110 1bbb xxx xxxx
Not affected
AC , Mx Mx & AC

Explanation
System movement of the instruction ANDM is almost the same as AND, but saving the
operation result in data memory as well as in accumulator.

20/126

Ver1.0

[ Example ] 0110B & 0101B


:
LDI
20H,0110B
LDI
21H,0101B
ANDM 20H,0
:
Instruction: ANDIM

Format:
Instruction Code:
Carry Flag:
Operation:

;AC=0110B,$20H=0110B
;AC=0101B,$21H=0101B
;AC=0100B,$20H=0100B

Function: to do logic AND operation with the value of Data Memory


and Immediate Data I, and then save the result in both
Accumulator and the Data Memory
ANDIM MxI
01110 iiii xxx xxxx
Not affected
AC , Mx Mx & I

Explanation
System movement of the instruction ANDIM is to change operand 2 (accumulator) of
instruction AND to immediate data I. This instruction is in immediate mode, so the address
of data memory can only be set to bank 0( $000H ~ $07FH ). The operation result is saved
in both accumulator and the data memory.
[ Example ] $20H=0110B , I=0011B
:
LDI
20H,0110B
;AC=06H,$20H=0110B
ANDIM 20H,0011B
;AC=0010B,$20H=0010B
:

Programming Tip
ANDIM itself has a special function MASK.
we can clear this bit to 0 with ANDIM like this:

When we need to set a certain bit to 0,

[ Example ] Clear bit 2 of $20H to 0


ANDIM

20H1011B

After execution: $20H=x0xxB

21/126

Ver1.0

Instruction: OR

Format:
Instruction Code:
Carry Flag:
Operation:

Function: to do logic OR operation with the values of Data Memory


and Accumulator, and then save the result in the
Accumulator
OR Mxbbb
00101 0bbb xxx xxxx
Not affected
AC Mx | AC

Explanation
In OR operation, the result will be 1(true) if either one of the two operands is 1(true).
Its logic table is as follows:
Logic operation table for OR
a
0
0
1
1

b
0
1
0
1

OR
0
1
1
1

However, in real instruction the logic operand also has 4bits rather than 1 bit. The
instruction OR is to OR the values of data memory with accumulator, and the result is saved
in the accumulator.
[ Example ] 0001B | 0100B
:
LDI
20H,0001B
LDI
21H,0100B
OR
20H,0
:
Instruction: ORM

Format:
Instruction Code:
Carry Flag:
Operation:

;$20H=0001B,AC=0001B
;$21H=0100B,AC=0100B
;$20H=0001B,AC=0101B

Function: to do logic OR operation with the values of Data Memory


and Accumulator, and then save the result in both the
Accumulator and the Data Memory
ORM Mxbbb
00101 1bbb xxx xxxx
Not affected
AC , Mx Mx | AC

Explanation
System movement of the instruction ORM is almost the same as OR, but saving the
operation result in data memory as well as in accumulator.
[ Example ] 0001B | 0100B
:
LDI
20H,0001B
LDI
21H,0100B
ORM
20H,0
:

;$20H=0001B,AC=0001B
;$21H=0100B,AC=0100B
;$20H=0101B,AC=0101B

22/126

Ver1.0

Programming Tip
In program designing, if a certain bit of a variable needs to be set to 1 and the other bits
must not be affected, then this can be done by the instruction OR. Because any bit that has
done OR operation with 0 can keep its original value, while those with 1 will have the value of
1.
Instruction: ORIM

Format:
Instruction Code:
Carry Flag:
Operation:

Function: to do logic OR operation with the value of Data Memory


and Immediate Data I, and then save the result in both
Accumulator and the Data Memory
ORIM MxI
01101 iiii xxx xxxx
Not affected
AC , Mx Mx | I

Explanation
System movement of the instruction ORIM is to do logic OR operation with the value of
data memory and Immediate Data I, and save the result in both accumulator and the data
memory. This instruction is also in immediate mode.
[ Example ] Set bit 3 of the value of $20H to 1
ORIM 20H1000B
After execution: $20H=1xxxB
Instruction: EOR

Format:
Instruction Code:
Carry Flag:
Operation:

Function: to do logic Exclusive OR operation with the values of Data


Memory and Accumulator, and then save the result in the
Accumulator
EOR Mxbbb
00100 0bbb xxx xxxx
Not affected
AC Mx ^ AC

Explanation
System movement of EOR is to do logic Exclusive OR operation with the values of data
memory and accumulator, and save the result in the accumulator. EOR is usually referred
to as Exclusive OR, because the operation result will be 1 only if the two operands have
different values; otherwise the result will be 0. The logic table for EOR is as follows:
Logic operation table for EOR
a
0
0
1
1

b
0
1
0
1

[ Example ] 0011B ^ 0101B


:
LDI
20H,0011B
LDI
21H,0101B
EOR
20H,0
:

EOR
0
1
1
0

;$20H=0011B,AC=0011B
;$21H=0101B,AC=0101B
;$20H=0011B,AC=0110B

23/126

Ver1.0

Instruction: EORM

Format:
Instruction Code:
Carry Flag:
Operation:

Function: to do logic Exclusive OR operation with the values of


Data Memory and Accumulator, and then save the result
in both the Accumulator and the Data Memory
EORM Mxbbb
00100 1bbb xxx xxxx
Not affected
AC , Mx Mx ^ AC

Explanation
System movement of the instruction EORM is almost the same as EOR, i.e. doing EOR
action with the values of data memory and accumulator, but saving the operation result in the
data memory as well as in the accumulator.

[ Example ] 0011B ^ 0101B


:
LDI
20H,0011B
LDI
21H,0101B
EORM 20H,0
:
Instruction: EORIM

Format:
Instruction Code:
Carry Flag:
Operation:

;$20H=0011B,AC=0011B
;$21H=0101B,AC=0101B
;$20H=0110B,AC=0110B

Function: to do logic EOR operation with the value of Data Memory


and Immediate Data I, and then save the result in both
the Accumulator and the Data Memory
EORIM MxI
01100 iiii xxx xxxx
Not affected
AC , Mx Mx ^ I

Explanation
Operand 2 of the instruction EORIM should be immediate data. This instruction is to do
logic EOR operation with the value of data memory and immediate data I, and to save the
result in both the accumulator and the data memory.
[ Example ] $20H=0011BI=0101B
:
LDI
20H,0011B
;$20H=0011B,AC=0011B
EORIM 20H,0101B
;$20H=0110B,AC=0110B
:

Programming Tip
After reading all of the logic instructions, you may wonder why there havent the NOT
instruction (inverse)? If there havent such an instruction in SH6610 series, what can I do?
Dont worry, Programming Tip is going to tell you how to use other instructions to perform the
NOT function. The operation of NOT is to change 0 1 or 1 0 on each bit. Here, the
EOR instruction can help us to get the inversed value by doing EOR operation with
immediate data (0FH) and the variable that wants to be done NOT. Youll understand
clearly after reading the following example.

24/126

Ver1.0

[ Example ] Do the operation NOT with the value of $20H (1100B)


EORIM

20H,0FH

Execution result: $20H=0011B,AC=0011B

1-6 Instructions for Flow Control


Instruction: JMP
Format:
Instruction Code:
Carry Flag:
Operation:

Function: to jump to a designated address to execute program


JMP
X
1110p xxxx xxx xxxx
Not affected
PC X(Include p)

Explanation
Instruction JMP jumps to a designated address to execute program. However,
addressing capability of SH6610 series CPU is limited to 4K words (0000H~0FFFH), so the
jumping range of JMP can only reach 4K(0FFFH). Address beyond 4K shall be reached by
switching banks. Theres detailed explanation in later chapters for how to switch banks.
The JMP instruction is similar to the GOTO instruction in BASIC program.
[ Example ] PC=40H , Jump to 0E00H
JMP

0E00H

Execution result: PC=0E00H

[ Example ] Jump to LABLE


JMP

LOOP
:
ORG 0340H
LOOP : NOP

;jump to loop (PC 0340h)

NOP
:
Instruction: BAZ

Format:
Instruction Code:
Carry Flag:
Operation:

Function: if AC equals 0, then go to a designated address to


execute program; otherwise continue to execute the next
line
BAZ
X
10010 xxxx xxx xxxx
Not affected
PC X , if AC=0

Explanation
If the value of AC is 0, then after executing the BAZ instruction, PC will go to the
designated address X to execute program, the range of X being from $000H to $7FFH. It
continues to execute the next line if the value of AC is 1

25/126

Ver1.0

[ Example ] if ($20H=$20H-1)=00H then goto INC21H

DEC20H:

INC21H

LDI
SBIM
BAZ
JMP
ADIM

Instruction: BA0

Format:
Instruction Code:
Carry Flag:
Operation:

20H,0FH
20H,01H
INC21H
DEC20H
:
21H,01H
:

;$20H=0FH
;AC,$20H $20H -1
;ifAC=0 jump to INC21H
;else jump to DEC20H
;$21H+1

Function: if bit 0 of AC is 1, then go to a designated address to


execute program; otherwise continue to execute the next
line
BA0
X
10100 xxxx xxx xxxx
Not affected
PC X , if AC(0)=1

Explanation
If bit 0 of AC is 1, then after executing BA0 instruction, PC will go to the designated
address X to execute program, the range of X being from $000H to $7FFH or from $0800H to
$0FFFH. It continues to execute the next line if bit 0 of AC is 0.

[ Example ] if $20H(bit 0)=1 then goto INC21H


:

DEC20H:

LDI

20H,0FH

;$20H=0FH,AC=0FH

BIM

20H,01H

;$20H,AC$20H -1

BA0

INC21H

;if AC(bit0)=1,jump to INC21H

JMP

DEC20H

;else jump to DEC20H

:
INC21H:

ADIM

21H,01H

;$21H,AC$21H+1

26/126

Ver1.0

Instruction: BA1

Format:
Instruction Code:
Carry Flag:
Operation:

Function: if bit 1 of AC is 1, then go to designated address to


execute program; otherwise continue to execute the next
line
BA1
X
10101 xxxx xxx xxxx
Not affected
PC X , if AC(1)=1

Explanation
If bit 1 of AC is 1, then after executing BA1 instruction, PC will go to the designated
address X to execute program, the range of X being from $000H to $7FFH or from $0800H to
$0FFFH. It continues to execute the next line if bit 1 of AC is 0.
[ Example ] if $20H(bit 1)=1 then goto INC21H
:
LDI
20H,0FH
;$20H=0FH,AC=0FH
DEC20H:
SBIM
20H,01H
;$20H,AC$20H -1
BA1
INC21H
;ifAC(bit1)=1,jump to INC21H
JMP
DEC20H
;else jump to DEC20H
:
INC21H: ADIM
21H,01H
;$21H,AC$21H+1
:
Instruction: BA2

Format:
Instruction Code:
Carry Flag:
Operation:

Function: if bit 2 of AC is 1, then go to designated address to


execute program; otherwise continue to execute the next
line
BA2
X
10110 xxxx xxx xxxx
Not affected
PC X , if AC(2)=1

Explanation
If bit 2 of AC is 1, then after executing BA2 instruction, PC will go to the designated
address X to execute program, the range of X being from $000H to $7FFH or from $0800H or
$0FFFH. It continues to execute the next line if bit 1 of AC is 0.

[ Example ] if $20H(bit 2)=1 then goto INC21H


:
LDI
20H,0FH
;$20H=0FH,AC=0FH
DEC20H:
SBIM
20H,01H
;$20H,AC$20H -1
BA2
INC21H
;if AC(bit2)=1,jump to INC21H
JMP
DEC20H
;else jump to DEC20H
:
INC21H:
ADIM
21H,01H
;$21H,AC$21H+1
:

27/126

Ver1.0

Instruction: BA3

Format:
Instruction Code:
Carry Flag:
Operation:

Function: if bit 3 of AC is 1, then go to designated address to


execute program; otherwise continue to execute the next
line
BA3
X
10111 xxxx xxx xxxx
Not affected
PC X , if AC(3)=1

Explanation
If bit 3 of AC is 1, then after executing BA3 instruction, PC will go to the designated
address X to execute program, the range of X being from $000H to $7FFH or from $0800H to
$0FFFH. It continues to execute the next line if bit 1 of AC is 0.

[ Example ] if $20H(bit 3)=1 then goto INC21H


:
LDI
20H,0FH
;$20H=0FH,AC=0FH
DEC20H
SBIM
20H,01H
;$20H,AC$20H -1
BA3
INC21H
;if AC(bit3)=1,jump to INC21H
JMP
DEC20H
;else jump to DEC20H
:
INC21H:
ADIM
21H,01H
;$21H,AC$21H+1
:
Instruction: BC
Format:
Instruction Code:
Carry Flag:
Operation:

Function: if CY is 1, then go to designated address to execute


program; otherwise continue to execute the next line
BC
X
10011 xxxx xxx xxxx
CY
PC X , if CY=1

Explanation
If CY is 1, then after executing instruction BC, PC will go to the designated address X to
execute program, the range of X being from $000H to $7FFH. It continues to execute the
next line if CY is not 1. The instruction BC is often used after addition or subtraction to
decide whether there is a carry or borrow. You should especially note that for addition, CY
is set to 1 when there is a carry, while for subtraction CY is set to1 when there isnt any
borrow. Therefore you should be careful when dealing with program flows.

[ Example ] if CY=1 then goto INC20H


:
LDI
20H,0FH
;$20H=0FH,AC=0FH,CY=0
INC20H :
SBIM
20H,01H
;$20H,AC$20H -1
BC
INC20H
;if CY=1,jump to INC20H
:

28/126

Ver1.0

Instruction: TJMP
Format:
Instruction Code:
Carry Flag:
Operation:

Function:Unconditionally go to the address composed


(PC11~PC8), TBR and AC value to execute program
TJMP
11110 1111 111 1111
Not affected

by

Explanation
The destination address for Instruction TJMP is composed by PCs bit 8~bit 11, TBR
and AC value (please refer to instruction RTNW for program example).
Example: PC=300H, TBR=01H, AC=02H, then the rule for composing a destination
address is as follows:
address=PC(bit8~bit 11) TBR AC
If:
PC =300H
TBR=01H
AC=02H
Then the destination address is: 3 1 2 H
Instruction: CALL
Format:
Instruction Code:
Carry Flag:
Operation:

Function: to call a subprogram


CALL X
11000 xxxx xxx xxxx
Not affected
ST CY;PC , PC X (not include p)

Explanation
Instruction CALL is used to call a subprogram. First it saves the values of CY and PC+1
to stack for returning to the calling program, then goes to the designated address X ( $0000H
~ $07FFH or $0800H ~ $0FFFH) to execute program. Instructions RTNW or RTNI can be
used to return to the calling program. When using CALL to call a subprogram, you should
especially note how many layers of stack have already been used, because SH6610 series
only provide 4-layer stacks. If more than 4 layers are used, serious error will be occurred
when returning to the calling program!
Instruction: RTNW
Format:
Instruction Code:
Carry Flag:
Operation:

Function: to return to the calling program, HTBR, LAC


RTNW HL
11010 000h hhh 1111
Not affected
PC ST , TBR hhhh , AC 1111

Explanation
RTNW is an instruction to get data from stack to PC for returning to the calling program,
and at the same time put the value of H into TBR and the value of L into AC. This
instruction is often used to get stationary data.

29/126

Ver1.0

[ Example ] To get data from ROM address 302H


TBR
TEMP

EQU
EQU

001A
001B
001C
001D

LDI
LDI
CALL

ORG
0300
TJMP
(PC11~PC8),TBR,AC
0301
RTNW
0302
RTNW
0303
RTNW
0304
RTNW
0305
Instruction: RTNI
Format:
Instruction Code:
Carry Flag:
Operation:

OEH
20H
:
:
TBR00H ;put index value (high nibble) 0 into TBR.
TEMP02 ;put index value (low nibble) 2 into AC
300H
;call subprogram.
:
:
:
300H
; get destination address $0302H according to
00H,01H
00H,02H
04H,05H
09H,08H
:

;return to main program, HTBR,LAC

Function: to return from interrupt or subprogram


RTNI
11010 1000 000 0000
CY
CY;PC ST

Explanation
Instruction RTNI is mainly used for returning from interrupt or subprogram. It fills CY
and PC with values of stack (CY and returning address) when returning. Whats the
difference between RTNI and RTNW? We can find that when returning by RTNW, only the
returning address in the stack is fetched into PC, but CY value is not fetched. And RTNW
fetches another two values (HTBR, LAC), which RTNI does not do. Therefore you can
choose from the two instructions according to your needs.
[ Example ] To exchange two numbers
000E
0020
0021
0022

1 TBR
2 REGX
3 REGY
4 TEMP
5 ;*********************
0005 780E 12 RESET :
0006 7920 13
0007 7A21 14
0008 C00A 15
17 ;**********************
000A 3820 18 SWAPXY
000B 3C22 19
000C 3821 20
000D 3C20 21
000E 3822 22
000F 3C21 23
0010 D400 24

EQU
EQU
EQU
EQU

0EH
20H
21H
22H

LDI
LDI
LDI
CALL

TEMP,00H
REGX,02H
REGY,04H
SWAPXY

LDA
STA
LDA
STA
LDA
STA
RTNI

REGX,00H
;AC=02H
TEMP,00H
;TEMP=02H
REGY,00H
;AC=04H
REGX,00H
;REGX=04H
TEMP,00H
;AC=02H
REGY,00H
;REG2=02H
;return to main program

30/126

;set TEMP=00h
;set RegX=02h
;set RegY=04h
; ;call subprogram

Ver1.0

Instruction: HALT
Format:
Instruction Code:
Carry Flag:
Operation:

Function: CPU to be halt from working


HALT
11011 0000 000 0000
Not affected
No

Explanation
After executing instruction HALT, CPU will be halt while its surrounding circuit (counter,
oscillation circuit) continues working. The instruction HALT is usually used to stop CPU
temporarily in order to save power. In HALT mode, when any of the system interrupts
occurs, CPU will be released from HALT mode and continue to work.
[ Example ] HALT program, to enable PORT B interrupt to wake up the program
IEX
EQU
IRQ
EQU
PORTB EQU
LDI
LDI
LDI
HALT
NOP

00H
01H
09H
:
PORTB,0FH
IEX,0001B
IRQ,00H

;interrupt enable register


;interrupt require flag
;i/o port b
;set port b = high
;enable port interrupt
;clear interrupt require flag
;system cpu halt

:
Instruction: STOP
Format:
Instruction Code:
Carry Flag:
Operation:

Function: to stop the whole chip (including oscillation circuit)


STOP
11011 1000 000 0000
Not affected
No

Explanation
Executing instruction STOP will stop the whole chip from working, including oscillation
circuit. Only PORT interrupt and external interrupt can release CPU from STOP mode, so
you must enable an interrupt before entering STOP mode, otherwise the system cant be
waked up from STOP mode.

31/126

Ver1.0

[ Example ] STOP program, to enable PORT B interrupt to wake up the program


IEX
EQU
IRQ
EQU
PORTB EQU
LDI
LDI
LDI
STOP
NOP

00H
01H
09H
:
PORTB,0FH
IEX,0001B
IRQ,00H

;interrupt enable register


;interrupt require flag
;i/o port b
;set port b = high
;enable port interrupt
;clear interrupt require flag
;all system is stop

:
Instruction: NOP
Format:
Instruction Code:
Carry Flag:
Operation:

Function: to do nothing
NOP
1111 1111 111 1111
Not affected
No

Explanation
Instruction NOP means doing nothing in its instruction cycle and it is often used for time
delay. Because it does nothing when executing, you dont worry if it will affect any current
status.

32/126

Ver1.0

CHAPTER TWO
System Software Application
In this chapter, we will use the SH6610 instructions explained in the first chapter to write
various application program, so that you can understand this system further more through
these application programs.

2-1

Data Transmission

The following programs mainly use indirect addressing mode to fill the data memory
(80H~85H) with desired values. In SH6610 system, the address of data memory exceeding
7FH can not be reached by immediate addressing, so the data can only be read and written
by indirect addressing mode.

[Example2-1.1] Data transfer


;*************************************************************************
;File name: USEDP.ASM
;Description: This program is data transfer program,that fill data to memory
;
($80h~$85h)
;Input Arguments: DPH,DPM,DPL,TMP
;*************************************************************************
;SYSTEM REGISTER TABLE DEFINE
;
;**************************
;
INX
EQU
0FH
;indirect address data r/w register
DPL
EQU
10H
;INX low nibble
DPM
EQU
11H
;INX middle nibble
DPH
EQU
12H
;INX high nibble
;**************************
;
;DATA MEMORY TABLE DEFINE ;
;**************************
;
TMP
EQU
20H
;variable register
;**************************
ORG
00H
;
JMP
RESET
;
NOP
;
NOP
;
NOP
;
NOP
;
;**************************
RESET:
LDI
TMP,05H
;set fill data
LDI
DPH,01H
;set INX address =$85h
LDI
DPM,00H
;
LDI
DPL,05H
;
LOOP:
LDA
TMP,00H
;get data
STA
INX,00H
;write to memory
SBIM
DPL,01H
;get next address
BC
LOOP
;if INX address >=$80h,jump to loop
;*************************************************************************

33/126

Ver1.0

2-2

4/8-Bit Addition

Addition has its absolute importance in program designing and is needed everywhere.
Here lists 4-bit and 8-bit addition programs for reference.
[Example2-2.1] 4-bit addition (5h+4h)
;************************************************************************
;File name: ADD4.ASM
;Description: This program is two 4 bit number addition program,
;
then save result to RUT,(RUT=X+Y)
;Input Arguments: X , Y
;Output Arguments: RUT
;************************************************************************
;DATA MEMORY TABLE DEFINE
;
;**************************
;
X
EQU
20H
;set be added "X" register
Y
EQU
21H
;set added "Y" register
RUT
EQU
22H
;set summation "RUT" register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
LDI
X,05H
;set X=05H
LDI
Y,04H
;set Y=04H
LDA
X,00H
ADD
Y,00H
;X+Y
STA
RUT,00H
;save (X+Y) result to RUT
END
;************************************************************************

34/126

Ver1.0

[Example2-2.2] 8-bit addition (1Fh+44h)


;************************************************************************
;File name: ADD8.ASM
;Description: This program is two 8 bit number addition program,
;
then save result to RUT_H,RUT_L.
;Input Arguments: X_H,X_L,Y_H,Y_L
;Output Arguments: RUT_H,RUT_L
;************************************************************************
;DATA MEMORY TABLE DEFINE
;
;**************************
;
X_H
EQU
20H
;augend X high 4 bits register
X_L
EQU
21H
;augend X low 4 bits register
Y_H
EQU
22H
;addend Y high 4 bits register
Y_L
EQU
23H
;addend Y low 4 bits register
RUT_H
EQU
24H
;summation RUT high 4 bits register
RUT_L
EQU
25H
;summation RUT low 4 bits register
;*************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
LDI
X_H,01H
;set augend = 1fh
LDI
X_L,0FH
LDI
Y_H,04H
;set addend = 44h
LDI
Y_L,04H
LDA
X_L,00H
ADD
Y_L,00H
;X low nibble add Y low nibble
STA
RUT_L,00H ;save to RUT low nibble
LDA
X_H,00H
ADC
Y_H,00H
;X high nibble add Y high nibble add CY
STA
RUT_H,00H ;save ti RUT high nibble
END

35/126

Ver1.0

2-3

4/8-Bit Subtraction

Subtraction is also used very often in program designing. 4-bit and 8-bit subtraction
examples are listed respectively in the following example. If you want to use subtraction over
8 bit, you can only accord to the example in your practical use.
[Example2-3.1] 4-bit subtraction (5H-4H)
;*************************************************************************
;File name: SUB4.ASM
;Description: This program is two 4 bit number subtraction program,then save
;
result to RUT,(RUT=X-Y)
;Input Arguments: X , Y
;Output Arguments: RUT
;*************************************************************************
;DATA MEMORY TABLE DEFINE
;**************************
X
EQU
20H
;minuend register
Y
EQU
21H
;subtrahend register
RUT
EQU
22H
;summation register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
LDI
X,05H
;set minuend X = 05h
LDI
Y,04H
;set subtrahend Y = 04h
LDA
Y,00H
;get subtrahend Y
SUB
X,00H
;X-Y
STA
RUT,00H
;save the result to RUT
;*************************************************************************

36/126

Ver1.0

[Example2-3.2] 8-bit subtraction (44H-1FH)


;*************************************************************************
;File name: SUB8.ASM
;Description: This program is two 8 bit number subtraction program,save
;
result to RUT_H,RUT_L,(RUT_H,RUT_L=(X_H,X_L)-(Y_H,H_L))
;Input Arguments: X_H,X_L,Y_H,Y_L
;Output Arguments: RUT_H,RUT_L
;*************************************************************************
;DATA MEMORY TABLE DEFINE
;
;**************************
;
X_H
EQU
20H
;minuend high 4 bits register
X_L
EQU
21H
;minuned low 4 bits register
Y_H
EQU
22H
;subtrahend high 4 bits register
Y_L
EQU
23H
;subtrahend low 4 bits register
RUT_H
EQU
24H
;summation high 4 bits register
RUT_L
EQU
25H
;summation low 4 bits register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
LDI
X_H,01H
;set minuend =1fh
LDI
X_L,0FH
LDI
Y_H,04H
;set subtrahend =44h
LDI
Y_L,04H
LDA
Y_L,00H
;get subtrahend low nibble data
SUB
X_L,00H
;low nibble subtraction
STA
RUT_L,00H
;save result to RUT_L
LDA
Y_H,00H
;get subtrahend high nibble data
SBC
X_H,00H
;high nibble subtraction
STA
RUT_H,00H
;save result to RUT_H
;*************************************************************************

37/126

Ver1.0

2-4

4/8-Bit Multiplication

There isnt any instruction for multiplication in SH6610 system, so you may wonder
how to do multiplication? In fact, although theres not such an instruction, we can still
perform this function with existing instructions. Most of multiplication instructions in other
systems are composed by addition and some judging instructions, and packaged by system
designing engineers. Therefore, we provide here with 4/8-bit program examples written in
this method for your reference:
[Example2-4.1] 4-bit multiplication
;*************************************************************************
;File name: MUL4.ASM
;Description: This program is two 4 bit number multiplication program,save
;
result to RUT_H,RUT_L
;Input Arguments: X , Y
;Output Arguments: RUT_H,RUT_L
;*************************************************************************
;DATA MEMORY TABLE DEFINE
;**************************
X
EQU
20H
;multiplicand register
Y
EQU
21H
;multiplier register
RUT_H
EQU
22H
;summation high 4 bits register
RUT_L
EQU
23H
;summation low 4 bits register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
LDI
X,05H
;set X = 05h
LDI
Y,04H
;set Y = 04h
LDI
RUT_L,00H
;set RUT_L = 00h
LDI
RUT_H,00H
;set RUT_H = 00h
;
AGAIN:
SBIM
Y,01H
;Y - 1 -->Y
BC
ADD_RUT_L
;Y>0 ? yes, go to ADD_RUT_L
JMP
MUL_OK
;no,end multiplication
;
ADD_RUT_L:
LDA
X,00H
;get multiplicand X
ADDM
RUT_L,00H
;add X to RUT_L
BC
ADD_RUT_H
;carry ? , yes ,goto ADD_RUT_H
JMP
AGAIN
;no,goto AGAIN
;
ADD_RUT_H:
ADIM
RUT_H,01H
;add CY to RUT_H
JMP
AGAIN
MUL_OK:
;*************************************************************************

38/126

Ver1.0

[Example2-4.2] 8-bit multiplication


;*************************************************************************
;File name: MUL8.ASM
;Description: This program is 8 bit Multiplication program, result
;
save to RUT_2,RUT_1,RUT_0
;
(MA1,MA0)x(MB1,MB0)-->(RUT_2,RUT_1,RUT_0)
;Input Arguments: MA1,MA0,MB1,MB0
;Output Arguments: RUT_2,RUT_1,RUT_0
;*************************************************************************
;DATA MEMORY TABLE DEFINE
;**************************
MA1
EQU
20H
;miltiplicand high 4 bits register
MA0
EQU
21H
;miltiplicand low 4 bits register
MB1
EQU
22H
;multiplier high 4 bits register
MB0
EQU
23H
;multiplier low 4 bits register
RUT_2
EQU
24H
;summation register2 (bit8~11)
RUT_1
EQU
25H
;summation register1 (bit4~7)
RUT_0
EQU
26H
;summation register0 (bit0~3)
TMP
EQU
27H
;variable register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
LDI
MA1,01H
;set MA1=01h
LDI
MA0,0FH
;set MA0=0fh
LDI
MB1,04H
;set MB1=04h
LDI
MB0,04H
;set MB0=04h
LDI
RUT_0,00H
;clear summation register0
LDI
RUT_1,00H
;clear summation register1
LDI
RUT_2,00H
;clear summation register2
;__________________________
AGAIN:
SBIM
MB0,01H
;check multiplication is ok ?
BC
ADD_CC
;if CY=1,thin jump to ADD_CC
SBIM
MB1,01H
;check multilication is ok ?
BC
ADD_CC
;if CY=1,then jump to ADD_CC
JMP
MUL_OK
;end multiplication
;
ADD_CC:
LDA
MA0,00H
;get data
ADDM
RUT_0,00H
;add MA0 to RUT_0
LDA
MA1,00H
;get data
ADCM
RUT_1,00H
;add CY and MA1 to RUT_1
LDI
TMP,00H
;clear AC
ADD_RUT_0:
ADCM
RUT_2,00H
;add CY to RUT_2
JMP
AGAIN
MUL_OK:
;************************************************************************

39/126

Ver1.0

2-5

4/8-Bit Division

There isnt an instruction for division in this SH6610 system, so we again need to use
existing instructions to perform this function. As you all know, division means the number of
times one number can be subtracted by another number. Therefore we can conclude that
division can be performed with subtraction and other instructions. We have two examples
that are relatively representative for 4/8-bit division:
[Example2-5.1] 4-bit division
;*************************************************************************
;File name: DIV4.ASM
;Description: This program is two 4 bit number division program,then save the
;
quotiert to QUO register , save remainder to REM register.
;Input Arguments: X , Y
;Output Arguments: QUO,REM
;*************************************************************************
;DATA MEMORY TABLE DEFINE
;
;**************************
;
X
EQU
20H
;dividend register
Y
EQU
21H
;divisor register
QUO
EQU
22H
;quotiert register
REM
EQU
23H
;remainder register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
LDI
X,0FH
;set X=0fh
LDI
Y,03H
;set Y=03h
LDI
QUO,00H
;clear QUO
LDI
REM,00H
;clear REM
;__________________________
AGAIN:
LDA
Y,00H
;get divisor number
SUBM
X,00H
;use subtraction to do divisor
BC
ADD_QUO
;X>Y ? ,yes,goto ADD_QUO
JMP
DIV_OK
;no,goto DIV_OK
ADD_QUO:
STA
REM,00H
;save remainder to REM
ADIM
QUO,01H
;quottiert + 1,save to QUO
JMP
AGAIN
DIV_OK:
;ending divisor
;************************************************************************

[Example2-5.2] 8-bit division


;*************************************************************************
;File name: DIV8.ASM
;Description: This program is two 8 bit number division program,then save the
40/126

Ver1.0

;
quotiert to QUO_H,QUO_L register , save remainder to REM_H,REM_L
;
register.
;Input Arguments: X_H ,X_L , Y_H , Y_L
;Output Arguments: QUO_H,QUO_L,REM_H,REM_L
;*************************************************************************
;DATA MEMORY TABLE DEFINE
;
;**************************
;
X_H
EQU
20H
;dividend high 4 bits register
X_L
EQU
21H
;dividend low 4 bits register
Y_H
EQU
22H
;divisor high 4 bits register
Y_L
EQU
23H
;divisor low 4 bits register
QUO_H
EQU
30H
;quotiert high 4 bits register
QUO_L
EQU
31H
;quotiert low 4 bits register
REM_H
EQU
32H
;remainder high 4 bits register
REM_L
EQU
33H
;remainder low 4 bits register
TMP
EQU
24H
;variable register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
LDI
X_H,0FH
;set divided =FFH
LDI
X_L,0FH
LDI
Y_H,01H
;set divisor =10H
LDI
Y_L,00H
LDI
QUO_L,00H
;clear QUO_L
LDI
QUO_H,00H
;clear QUO_H
LDI
REM_L,00H
;clear REM_L
LDI
REM_H,00H
;clear REM_H
;__________________________
AGAIN:
LDA
Y_L,00H
;get divisor low nibble
SUBM
X_L,00H
;division low nibble
;
LDA
Y_H,00H
;get divisor high nibble
SBCM
X_H,00H
;division high nibble
BC
ADD_QUO_L
;divided > divisor ? yes,goto
JMP
DIV_OK
;no,goto DIV_OK
;
ADD_QUO_L:
STA
REM_H,00H
;save remainder high nibble to
;
LDA
X_L,00H
;get remainder low nibble
STA
REM_L,00H
;save remainder low nibble to REM_L
;
ADIM
QUO_L,01H
;quotiert low nibble +1
LDI
TMP,00H
;clear AC=0
ADCM
QUO_H,00H
;add CY to QUO_H
JMP
AGAIN
;
DIV_OK:
;
;*************************************************************************

41/126

Ver1.0

2-6

4/8-Bit Left Move by n bit(s) (n=1~3/7)

In the following example, the main function is to do a left movement by n bit(s) for a 4/8-bit
value. However, there isnt such a specific instruction provided in SH6610 system. So
according to the principle of left movement, moving left by n bit(s) is equal to being multiplied
by 2n, and the multiplied result is equal to the result from moving left by n bit(s).
[Example 2-6.1] 4-bit left move by n bit (s) (n=1~3)
;*************************************************************************
;File name: LEFT4.ASM
;Description: This program is 4 bit left program .
;Input Arguments: LFT,N
;Output Arguments: RUT
;*************************************************************************
;DATA MEMORY TABLE DEFINE
;
;**************************
LFT
EQU
20H
;left register
RUT
EQU
22H
;result register
N
EQU
23H
;variable register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
LDI
LFT,1010B
;set left number LFT=1010b
LDI
N,01H
;left 1 bit
11
CALL
SHIFT4
;call shift4 program
LDI
LFT,1010B
;set left number LFT=1010b
LDI
N,02H
;left 2 bit
CALL
SHIFT4
LDI
LFT,1010B
;set left number LFT=1010b
LDI
N,03H
;left 3 bit
CALL
SHIFT4
JMP
$
;**************************
;SHIFT LEFT N BIT SUB PROGRAM
;**************************
SHIFT4
SBIM
N,01H
;N>0,then goto LEFT
1
BC
LEFT
;else goto ENDLEFT
JMP
ENDLEFT
;
LEFT:
LDA
LFT,00H
;get left data
ADDM
LFT,00H
;left one bit
STA
RUT,00H
;save to RUT
JMP
SHIFT4
;
ENDLEFT:
RTNI
;return to main program
;*************************************************************************

42/126

Ver1.0

[Example2-6.2] 8-bit left move by n bit (s) (n=1~7)


;*************************************************************************
;File name: LEFT8.ASM
;Description: This program is 8 bit left program .
;Input Arguments: LFT_H,LFT_L,N
;Output Arguments: RUT_H,RUT_L
;*************************************************************************
;DATA MEMORY TABLE DEFINE
;
;**************************
LFT_H
EQU
20H
;left high nibble register
LFT_L
EQU
21H
;left low nibble register
N
EQU
24H
;variable register(N=1~7)
RUT_H
EQU
25H
;left result high nibble register
RUT_L
EQU
26H
;left result low nibble register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
LDI
LFT_H,1010B
;set left number=10100011b
LDI
LFT_L,0011B
LDI
N,01H
;left 1 bit
CALL
SHIFT8
;call shift8 program
LDI
LFT_H,1010B
;set left number=10100011b
LDI
LFT_L,0011B
LDI
N,02H
;left 2 bit
CALL
SHIFT8
;call shift8 program
LDI
LFT_H,1010B
;set left number=10100011b
LDI
LFT_L,0011B
;
LDI
N,05H
;left 5 bit
CALL
SHIFT8
;call shift8 program
LDI
LFT_H,1010B
;set left number=10100011b
LDI
LFT_L,0011B
LDI
N,07H
;left 7 bit
CALL
SHIFT8
;call shift8 program
JMP
$
;**************************
;SHIFT LEFT N BIT SUB PROGRAM
;**************************
SHIFT8
SBIM
N,01H
;N-1 to judge left is ok ?
BC
LEFT
;N>0,jump to LEFT
JMP
ENDLEFT
;N<0,left ok! jump to ENDLEFT
LEFT:
LDA
LFT_L,00H
;left low nibble one bit
ADDM
LFT_L,00H
STA
RUT_L,00H
;save result low nibble to RUT_L
LDA
LFT_H,00H
;left high nibble one bit
ADCM
LFT_H,00H
STA
RUT_H,00H
;save result high nibble to RUT_H
JMP
SHIFT8
;jump to left again
43/126

Ver1.0

ENDLEFT:
RTNI
;return to main program
;*************************************************************************

2-7

4/8-Bit Right Move by n bit(s) (n=1~3/7)

In the following example, the main function is to do a right movement by n bit(s) for a
4/8-bit value. However, there isnt such a specific instruction provided in SH6610 system.
So according to the principle of right movement, moving right by n bit(s) is equal to being
divided by 2n, and the divided result is equal to the result from moving right by n bit(s).
[Example2-7.1] 4-bit right move by n bit(s) (n=1~3)
;*************************************************************************
;File name: RIGHT4.ASM
;Description: This program is 4 bit right program .
;Input Arguments: RIGB, N
;Output Arguments: RESB
;*************************************************************************
;DATA MEMORY TABLE DEFINE
;
;**************************
;
RIGB
EQU
20H
;right register
VAR1
EQU
21H
;variable 1 register
RESB
EQU
22H
;result register
N
EQU
23H
;N=1~3
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
LDI
RIGB,1010B
;set right number=1010b
LDI
N,01H
;right 1 bit
CALL
RIGHT4
;call right4 origram
LDI
RIGB,1010B
;set right number=1010b
LDI
N,02H
;right 2 bit
CALL
RIGHT4
;call right4 origram
LDI
RIGB,1010B
;set right number=1010b
LDI
N,03H
;right 3 bit
CALL
RIGHT4
;call right4 origram
JMP
$
;**************************
;SHIFT RIGHT N BIT SUB PROGRAM
;**************************
RIGHT4
LDI
VAR1,02H
;set divid number
RIGHT:
SBIM
N,01H
;if n-1<0,then right ok!
BC
AGAIN1
;else right 1 bit
JMP
ENDRIGH
;jump to ENDRIGH
;__________________________
AGAIN1:
LDI
RESB,00H
;divid 2 to get right 1 bit
AGAIN:
LDA
VAR1,00H
SUBM
RIGB,00H
BC
AD_RESB
LDA
RESB,00H
;get right N-1 bit number
STA
RIGB,00H
;save to RIGB
JMP
RIGHT
AD_RESB:
ADIM
RESB,01H
;RESB add one
44/126

Ver1.0

AGAIN
;right again
JMP
ENDRIGH:
RTNI
;return to main program
*************************************************************

45/126

Ver1.0

[Example2-7.2] 8-bit right move by n bit(s) (n=1~7)


;*************************************************************************
;File name: RIGHT8.ASM
;Description: This program is 8 bit right program .
;Input Arguments: RIG1,RIG0,N
;Output Arguments: RESH,RESL
;*************************************************************************
;DATA MEMORY TABLE DEFINE
;**************************
RIG1
EQU
20H
;right high nibble register
RIG0
EQU
21H
;right low nibble register
VAR1
EQU
22H
;variable register
RESH
EQU
23H
;result high nibble register
RESL
EQU
24H
;result low nibble register
N
EQU
25H
;variable N register N=1~7
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
LDI
RIG1,1010B
;set right number=10101111b
LDI
RIG0,1111B
LDI
N,01H
;right 1 bit
CALL
RIGHT8
;call right8 program
LDI
RIG1,1010B
;set right number=10101111b
LDI
RIG0,1111B
LDI
N,02H
;right 2 bit
CALL
RIGHT8
;call right8 program
LDI
RIG1,1010B
;set right number=10101111b
LDI
RIG0,1111B
LDI
N,07H
;right 7 bit
CALL
RIGHT8
;call right8 program
JMP
$
;**************************
;SHIFT RIGHT N BIT SUB PROGRAM
;**************************
RIGHT8
LDI
VAR1,02H
;set VAR1=2(to divide 2)
RIGHT:
SBIM
N,01H
;N=0 ? ,yes,right ok!
BC
AGAIN1
;no, right 1 bit
JMP
ENDRIGH
;jump to ENDRIGH
;__________________________
AGAIN1:
LDI
RESH,00H
;clear RESH,RESL
LDI
RESL,00H
AGAIN:
LDA
VAR1,00H
;divide 2 to right 1 bit
SUBM
RIG0,00H
;divide low nibble
BC
ADD_RESL
SBIM
RIG1,01H
;divide high nibble
BC
ADD_RESL
LDA
RESH,00H
;RESH-->RIG1
STA
RIG1,00H
46/126

Ver1.0

LDA
RESL,00H
;RESL-->RIG0
STA
RIG0,00H
JMP
RIGHT
ADD_RESL:
ADIM
RESL,01H
;quotient low nibble + 1
BC
ADD_RESH
LDI
TMP,0
;clear AC
ADCM
RESH,01H
;quotient high nibble + 1
JMP
AGAIN
ENDRIGH:
RTNI
;return to main program
;*************************************************************************

47/126

Ver1.0

2-8 8-Bit Up/Down Counter


Because SH6610 is a 4-bit system itself, designing an 8-bit counter requires 2 nibbles.
Up counter can count from 00H to FFH, and down counter can count from FFH down to 00H.
[Example2-8.1] 8-bit up counter
;*************************************************************************
;File name: UP8.ASM
;Description: This program is 8 bit up counter program,00H-->FFH
;Input Arguments: CNT1,CNT0
;*************************************************************************
;DATA MEMORY TABLE DEFINE
;
;**************************
;
CNT1
EQU
20H
;counter high nibble register
CNT0
EQU
21H
;counter low nibble register
;**************************
ORG
00H
JMP
RESET
ORG
05H
;**************************
RESET:
LDI
CNT1,00H
;set counter inition =00h
LDI
CNT0,00H
;
UPCNT0:
ADIM
CNT0,01H
;counter up one
BC
UPCNT1
;
SBI
CNT1,0FH
;*******
BAZ
$+2
;counter=ffh ?,yes,stop count
JMP
UPCNT0
;no,goto UPCNT0
SBI
CNT0,0FH
BAZ
END_UP
;*******
JMP
UPCNT0
;
UPCNT1:
ADIM
CNT1,01H
;counter up one
JMP
UPCNT0
END_UP:
JMP
$
;*************************************************************************

48/126

Ver1.0

[Example2-8.2] 8-bit down counter


;*************************************************************************
;File name: DOWN8.ASM
;Description: This program is 8 bit down counter program,FFH-->00H
;Input Arguments: CNT1,CNT0
;*************************************************************************
;DATA MEMORY TABLE DEFINE
;**************************
CNT1
EQU
20H
;counter high nibble register
CNT0
EQU
21H
;counter low nibble register
;**************************
ORG
00H
JMP
RESET
ORG
05H
;**************************
RESET:
LDI
CNT1,0FH
;set counter inition =ffh
LDI
CNT0,0FH
;
DNCNT0:
LDA
CNT0,00H
;get counter number low nibble
BAZ
$+2
;counter =00h? yes,goto check high
nibble
JMP
$+3
;no,counter down one
;
LDA
CNT1,00H
;get counter number high nibble
BAZ
END_DN
;cunter =00h? yes,goto END_DN
;
SBIM
CNT0,01H
;counter down
BC
DNCNT0
SBIM
CNT1,01H
BC
DNCNT0
END_DN:
JMP
$
;*************************************************************************

49/126

Ver1.0

2-9

Conversion Between Decimal and Binary

[Example2-9.1] BinaryBCD
;*************************************************************************
;File name: BINBCD.ASM
;Description: This program is BINARY code convert to BCD program,result
;
save to BDC2,BCD1,BCD0
;Input Arguments: BIN1 ,BIN0
;Output Arguments: BCD2, BCD1, BCD0
;*************************************************************************
;DATA MEMORY TABLE DEFINE
;
;**************************
BIN1
EQU
20H
;Binary high nibble register
BIN0
EQU
21H
;Binary low nibble register
BCD2
EQU
22H
;BCD hundred register
BCD1
EQU
23H
;BCD ten register
BCD0
EQU
24H
;BCD number register
TEMP
EQU
25H
;variable register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
;_____________15H=21_________
LDI
BIN1,01H
;set binary = 15h
LDI
BIN0,05H
CALL
CHANGE
;call conversion program
;_____________TEMP=80__________
LDI
BIN1,05H
;set binary = TEMP
LDI
BIN0,00H
CALL
CHANGE
;call conversion program
;_____________FFH=255_________
LDI
BIN1,0FH
;set binary = FFh
LDI
BIN0,0FH
CALL
CHANGE
;call conversion program
JMP
$
;***************************
;Binary to BCD conversion program
;***************************
CHANGE:
LDI
BCD2,00H
;clear result register
LDI
BCD1,00H
LDI
BCD0,00H
;
LDA
BIN0,00H
;get binary code
ADDM
BCD0,00H
;add Binary low nibble to BCD0
DAA
BCD0
LDI
TEMP,00H
;clear AC
ADCM
BCD1,00H
;add CY to BCD1
DOCG:
SBIM
BIN1,01H
;BIN1-1 >0 ?
BC
CG2
;yes,goto CG2
JMP
ENDCH
;no,goto ENDCH
50/126

Ver1.0

CG2:

ADIM
BCD0,06H
;add 16 to result
DAA
BCD0
LDI
TEMP,00H
;clear AC
ADCM
BCD1,00H
;add CY to BCD1
DAA
BCD1
;adjust BCD1
LDI
TEMP,00H
;clear AC
ADCM
BCD2,00H
;add CY to BCD2
ADIM
BCD1,01H
DAA
BCD1
;adjust BCD1
LDI
TEMP,00H
;clear AC
ADCM
BCD2,00H
;add CY to BCD2
JMP
DOCG
ENDCH:
RTNI
;return to main program
;*************************************************************************

51/126

Ver1.0

[Example2-9.2] BCD Binary


*************************************************************************
;File name: BCDBIN.ASM
;Description: This program is BCD (0-99) convert to BINARY program
;
,result high nibble save to BIN1,low nibble save to BIN0
;Input Arguments: DEC1,DEC0
;Output Arguments: BIN1, BIN0
;*************************************************************************
;DATA MEMORY TABLE DEFINE
;**************************
DEC1
EQU
20H
;Decimal high nibble register
DEC0
EQU
21H
;Decimal low nibble register
BIN1
EQU
22H
;Binary high nibble register
BIN0
EQU
23H
;Binary low nibble register
TMP
EQU
24H
;variable register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
;_____________15=0FH_________
LDI
DEC1,1
;set Decimal =15
LDI
DEC0,5
CALL
CHANGE
;call conversion program
;_____________50=32H_________
LDI
DEC1,5
;set Decimal =50
LDI
DEC0,0
CALL
CHANGE
;call conversion program
;_____________99=63H_________
LDI
DEC1,9
;set Decimal =99
LDI
DEC0,9
CALL
CHANGE
;call conversion program
JMP
$
;***************************
;BCD to Binary conversion program
;***************************
CHANGE:
LDI
BIN1,0
;clear BIN1
LDI
BIN0,0
;clear BIN0
;
LDA
DEC0,00H
;convert BCD low nibble to
;BINARY
ADDM
BIN0,00H
;save result to BIN0
;
DOCG
SBIM
DEC1,01H
;convert BCD high nibble to
;BINARY
BC
CG1
;save result to BIN1
JMP
ENDCH
;
CG1:
ADIM
BIN0,0AH
LDI
TMP,0
;clear AC
ADCM
BIN1,01H
52/126

Ver1.0

JMP
DOCG
ENDCH:
RTNI
;************************************************************************

53/126

Ver1.0

2-10

Square Operation

In this application program, we look up the square number in a Table. The use of Table
is very important in this system, and we usually get the required data by looking up in a table
especially when storing and taking a large quantum of data.
[Example2-10.1] Looking up in a table to get the square numbers (for 0-9)
;*************************************************************************
;File name: SQUARE.ASM
;Description: This program use Table to get "0-9" square numbers,
;
result savt to SQU_H,SQU_L
;Input Arguments: TMP
;Output Arguments: SQU_H,SQU_L
;*************************************************************************
;REGISTER TABLE DEFINE
;**************************
TBR
EQU
0EH
;**************************
;RAM REGISTER TABLE
;**************************
TMP
EQU
20H
;variable register
SQU_H
EQU
21H
;square numbers high nibble register
SQU_L
EQU
22H
;square numbers low nibble register
AC_B
EQU
23H
;AC buffer register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
;______________________________
LDI
TMP,00H
;get "0" square numbers
CALL
GET_SQU
;______________________________
LDI
TMP,02H
;get "2" square numbers
CALL
GET_SQU
;______________________________
LDI
TMP,04H
;get "4" square numbers
CALL
GET_SQU
;______________________________
LDI
TMP,06H
;get "6" square numbers
CALL
GET_SQU
;______________________________
LDI
TMP,09H
;get "9" square numbers
CALL
GET_SQU
JMP
$
;******************************
;get square numbers sub program
;******************************
GET_SQU:
LDI
TBR,00H
;set TJMP address
LDI
AC_B,01H
LDA
TMP,00H
;get TMP
ADD
AC_B,00H
54/126

Ver1.0

CALL
STA
LDA
STA

0500H
SQU_L,00H
TBR,00H
SQU_H,00H

;call sub program


;save square numbers low nibble to SQU_L
;get square numbers high nibble
;save square numbers high nibble to
SQU_H

RTNI
;*******************************
ORG
0500H
;square numbers data Table
TJMP
;jump by TJMP address ,to get data
RTNW
00H,00H
;0 ;return data
RTNW
00H,01H
;1
RTNW
00H,04H
;2
RTNW
00H,09H
;3
RTNW
01H,06H
;4
RTNW
02H,05H
;5
RTNW
03H,06H
;6
RTNW
04H,09H
;7
RTNW
06H,04H
;8
RTNW
08H,01H
;9
;************************************************************************

55/126

Ver1.0

2-11 Conversion Between ASCII And Hex


[Example2-11.1] ASCII Hex
;************************************************************************
;File name: ASCHEX.ASM
;Description: This program is ASCII code("0-F") convert to HEX
;
program , result save to (HEX1,HEX0).
;Input Arguments: ASC
;Output Arguments: HEX1,HEX0
;************************************************************************
;DATA MEMORY TABLE DEFINE
;**************************
ASC
EQU
20H
;ASCII register
HEX1
EQU
21H
;HEX high nibble register
HEX0
EQU
22H
;HEX low nibble register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
;____________0-->30H________
LDI
ASC,00H
;set ASCII code "0"
CALL
ASCHEX
;call conversion program
;____________6-->36H________
LDI
ASC,06H
;set ASCII code "6"
CALL
ASCHEX
;call conversion program
;____________9-->39H________
LDI
ASC,09H
;set ASCII code "9"
CALL
ASCHEX
;call conversion program
;____________A-->41H________
LDI
ASC,0AH
;set ASCII code "A"
CALL
ASCHEX
;call conversion program
;____________F-->46H________
LDI
ASC,0FH
;set ASCII code "F"
CALL
ASCHEX
;call conversion program
JMP
$
;**************************
;ASCII to HEX conversion sub program
;**************************
ASCHEX:
SBI
ASC,0AH
;if (ASC >0Ah),then
BC
CG1
;jump to CG1
LDI
HEX1,03H
;else,set (HEX1,HEX0)=30h
LDI
HEX0,00H
LDA
ASC,00H
;convert ASCII"0-9" to HEX
ADDM
HEX0,00H
;HEX high nibble save to HEX1
JMP
ENDCH
;low nibble save to HEX0
;
CG1:
LDI
HEX1,04H
;set (HEX1,HEX0)=41h
LDI
HEX0,01H
;
SBIM
ASC,0AH
;convert ASCII "A-F" to HEX,
56/126

Ver1.0

ADDM
JMP

HEX0,00H
ENDCH

; HEX high nibble save to HEX1,


; low nibble save to HEX0

;
ENDCH:
RTNI
;return main program
;*************************************************************************

57/126

Ver1.0

[Example2-11.2] Hex ASCII


;************************************************************************
;File name: ASCHEX.ASM
;Description: This program is ASCII code("0-F") convert to HEX
;
program , result save to (HEX1,HEX0).
;Input Arguments: ASC
;Output Arguments: HEX1,HEX0
;************************************************************************
;DATA MEMORY TABLE DEFINE
;**************************
ASC
EQU
20H
;ASCII register
HEX1
EQU
21H
;HEX high nibble register
HEX0
EQU
22H
;HEX low nibble register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
;____________0-->30H________
LDI
ASC,00H
CALL
ASCHEX
;____________6-->36H________
LDI
ASC,06H
CALL
ASCHEX
;____________9-->39H________
LDI
ASC,09H
CALL
ASCHEX
;____________A-->41H________
LDI
ASC,0AH
CALL
ASCHEX
;____________F-->46H________
LDI
ASC,0FH
CALL
ASCHEX
JMP
$
;**************************
;ASCII to HEX conversion sub program
;**************************
ASCHEX:
SBI
ASC,0AH
BC
CG1
LDI
HEX1,03H
LDI
HEX0,00H
LDA
ASC,00H
ADDM
HEX0,00H
JMP
ENDCH
;
CG1:
LDI
HEX1,04H
LDI
HEX0,01H
;
SBIM
ASC,0AH
ADDM
HEX0,00H
JMP
ENDCH
ENDCH:
RTNI

;set ASCII code "0"


;call conversion program
;set ASCII code "6"
;call conversion program
;set ASCII code "9"
;call conversion program
;set ASCII code "A"
;call conversion program
;set ASCII code "F"
;call conversion program

;if (ASC >0Ah),then


;jump to CG1
;else,set (HEX1,HEX0)=30h
;convert ASCII"0-9" to HEX
; HEX high nibble save to HEX1
; low nibble save to HEX0
;set (HEX1,HEX0)=41h

;convert ASCII "A-F" to HEX


; high HEX nibble save to HEX1
; low nibble save to HEX0
;return main program
58/126

Ver1.0

;*************************************************************************

59/126

Ver1.0

2-12

Conversion Between ASCII And Decimal

[Example2-12.1] ASCII Decimal


;************************************************************************
;File name: ASCDEC.ASM
;Description: This program is ASCII code convert to Decimal
;
program "0-9"-->48~57,"A-F"-->65~70
;Input Arguments: ASC
;Output Arguments: DEC1, DEC0
;************************************************************************
ASC
EQU
20H
;ASCII register
DEC1
EQU
21H
;Decimal high nibble register
DEC0
EQU
22H
;Decimal low nibble register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
;____________0-->48 ________
LDI
ASC,00H
;set ASCII code "0" to ASC
CALL
CHANGE
;call conversion program
;____________6-->54 ________
LDI
ASC,06H
;set ASCII code "6" to ASC
CALL
CHANGE
;call conversion program
;____________9-->57 ________
LDI
ASC,09H
;set ASCII code "9" to ASC
CALL
CHANGE
;call conversion program
;____________A-->65 ________
LDI
ASC,0AH
;set ASCII code "A" to ASC
CALL
CHANGE
;call conversion program
;____________F-->70 ________
LDI
ASC,0FH
;set ASCII code "F" to ASC
CALL
CHANGE
;call conversion program
JMP
$
;*********************************
; ASCII TO Decimal sub program
;conversion sub program
;*********************************
CHANGE:
SBI
ASC,0AH
BC
CG1
;if (ASC-0Ah)>0,then jump to CG1
LDI
DEC1,4
;else set Decimal=48
LDI
DEC0,8
;
LDA
ASC,00H
;convert ASCII(0-9) to Decimal
ADDM
DEC0,00H
;save Decimal high nibble to DEC1
DAA
DEC0
;save Decimal low nibble to DEC0
LDI
ASC,0
ADCM
DEC1,00H
JMP
ENDCH
;
CG1:
LDI
DEC1,06H
;set Decimal =65
LDI
DEC0,05H
;
60/126

Ver1.0

SBIM
ASC,0AH
;convert ASCII(A-F) to Decimal
ADDM
DEC0,00H
;save Decimal high nibble to DEC1
DAA
DEC0
;save Decimal low nibble to DEC0
LDI
ASC,0
ADCM
DEC1,00H
ENDCH:
RTNI
;return to main program
;***********************************************************************

61/126

Ver1.0

[Example2-12.2] Decimal ASCII


;*************************************************************************
;File name: DECASC.ASM
;Description: This program is Decimal(48~57) convert to ASCII code program,
;
result save to ASC.
;Input Arguments: DEC1,DEC0
;Output Arguments: ASC
;*************************************************************************
;DATA MEMORY TABLE DEFINE
;**************************
DEC1
EQU
20H
;set Decimal high nibble register
DEC0
EQU
21H
;set Decimal low nibble register
ASC
EQU
22H
;set ASCII register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
;____________48-->0 ________
LDI
DEC1,4
;set Decimal =48
LDI
DEC0,8
CALL
CHANGE
;call conversion program
;____________54-->6 ________
LDI
DEC1,5
;set Decimal =54
LDI
DEC0,4
CALL
CHANGE
;call conversion program
JMP
$
;*********************************
; ASCII TO Decimal sub program
;*********************************
CHANGE:
SBIM
DEC0,08H
;DEC0-08h to get ASCII code
DAS
DEC0
;adjust DEC0
STA
ASC,00H
;save result to ASC
;
ENDCH:
RTNI
;return to main program
;*************************************************************************

62/126

Ver1.0

2-13

Conversion From Decimal To 7 Segment Display

[Example2-13.1] Decimal Seven segment


;*************************************************************************
;File name: DSCLCD7.ASM
;Description: This program is use Table to get Deciaml LED display code
;Input Arguments: TMP, N
;Output Arguments: COD_H,COD_L
;*************************************************************************
;SYSTEM REGISTER TABLE DEFINE
;**************************
TBR
EQU
0EH
;**************************
;DATA MEMORY TABLE DEFINE
;**************************
TMP
EQU
20H
;variable register
COD_H
EQU
21H
;seven segmemt code high nibble
;register
COD_L
EQU
22H
;seven segmemt code low nibble
register
N
EQU
23H
;N register N=1(anode) N=0(cathode)
AC_B
EQU
24H
;AC buffer register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
;**************************
;get common cathode seven
;
;segment hex code
;
;**************************
;
LDI
TMP,04H
;get "4" of seven segmemt code
LDI
N,0
;set N=0,to get cathode seven
;segmemt
CALL
SEVCODE
;code,then call SEVCODE sub
program
;**************************
;get common anode seven
;segment hex code
;**************************
LDI
TMP,04H
;get "4" of seven segmemt code
LDI
N,1
;set N=1,to get anode seven segmemt
CALL
SEVCODE
;code,then call SEVCODE sub
program
JMP
$
;**************************
;get seven segment display code
;sub program
;**************************
SEVCODE:
LDI
AC_B,01H
;set TJMP address
LDA
N,00H
STA
TBR,00H
63/126

Ver1.0

LDA
ADDM
CALL
STA
LDA
STA

TMP,00H
AC_B,00H
0500H
COD_L,00H
TBR,00H
COD_H,00H

ENDCH:
RTNI
;
;*********************************
;seven segment hex code data area
;*********************************
ORG
0500H
TJMP
;__________common cathode table__
RTNW
03H,0FH

;get data
;save LED code low nibble to COD_L
;save LED code high nibble to
;COD_H
;return to main program

;use TJMP to get data


;0 ;return LED code
;(AC=0eh,TBR=07h)
;1
;2
;3
;4
;5
;6
;7
;8
;9

RTNW
00H,06H
RTNW
05H,0BH
RTNW
04H,0FH
RTNW
06H,06H
RTNW
06H,0DH
RTNW
07H,0DH
RTNW
00H,07H
RTNW
07H,0FH
RTNW
06H,0FH
;__________common anode table____
ORG
0510H
RTNW
0CH,00H
RTNW
0FH,09H
RTNW
0AH,04H
RTNW
0BH,00H
RTNW
09H,09H
RTNW
09H,02H
RTNW
08H,02H
RTNW
0FH,08H
RTNW
08H,00H
RTNW
09H,00H

;0
;1
;2
;3
;4
;5
;6
;7
;8
;9

64/126

Ver1.0

2-14

BCD 4/8-Bit Addition

[Example2-14.1]BCD 4 Bit Addition


;***********************************************************************
;File name: BCDADD4.ASM
;Description: This program is two 4 bit BCD number(X,Y) addition
;
program, result high nibble save to SUM1,low nibble
;
save to SUM0
;Input Arguments: X , Y
;Output Arguments: SUM1, SUM0
;************************************************************************
;DATA MEMORY TABLE DEFINE
;
;**************************
X
EQU
20H
;augend register
Y
EQU
21H
;addend register
SUM1
EQU
22H
;summation high nibble register
SUM0
EQU
23H
;summation low nibble register
TMP
EQU
24H
;variable register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
;____________5+4=9_____________
LDI
X,05H
;set X=5
LDI
Y,04H
;set Y=4
CALL
ADDTION
;call addition sub program
;____________8+8=16____________
;
LDI
X,08H
;set X=8
LDI
Y,08H
;set Y=8
CALL
ADDTION
;call addition sub program
JMP
$
;**************************
; BCD 4 BIT ADDTION SUB PROGRAM
;**************************
ADDTION:
LDI
SUM1,00H
LDI
SUM0,00H
LDA
X,00H
ADDM
Y,00H
;X+Y
DAA
Y
;adjust AC
STA
SUM0,00H
;save summation low nibble to SUM0
LDI
TMP,0
ADCM
SUM1,00H
;save summation high nibble to SUM1
ENDADD:
RTNI
;***********************************************************************

65/126

Ver1.0

[Example2-14.2] BCD 8 Bit Addition


;*************************************************************************
;File name: BCDADD8.ASM
;Description: This program is two 8 bit BCD number addition program,result
;
high nibble save to SUM2,middle nibble save to SUM1,low nibble
;
save to SUM0.
;Input Arguments: AUG_H,AUG_L,ADD_H,ADD_L
;Output Arguments: SUM2,SUM1,SUM0
;*************************************************************************
;DATA MEMORY TABLE DEFINE
;**************************
AUG_H
EQU
20H
;augend high nibble register
AUG_L
EQU
21H
;augend low nibble register
ADD_H
EQU
22H
;addend high nibble register
ADD_L
EQU
23H
;addend low nibble register
SUM2
EQU
24H
;summation high nibble register
SUM1
EQU
25H
;summation middle nibble register
SUM0
EQU
26H
;summation low nibble register
TMP
EQU
27H
;variable register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
;____________54+54=108 ________
LDI
AUG_H,5
;set augend =54
LDI
AUG_L,4
LDI
ADD_H,5
;set addend =54
LDI
ADD_L,4
CALL
ADDTION
;call addition program
;____________99+99=198_________
LDI
AUG_H,9
;set augend =99
LDI
AUG_L,9
LDI
ADD_H,9
;set addend =99
LDI
ADD_L,9
CALL
ADDTION
;call addition program
JMP
$
;**************************
; BCD 8 BIT ADDTION SUB PROGRAM
;**************************
ADDTION:
LDI
SUM2,00H
;clear SUM2
LDI
SUM1,00H
;clear SUM1
LDI
SUM0,00H
;clear SUM0
;
ADDL:
LDA
AUG_L,00H
;two number low nibble addition,result
ADDM
ADD_L,00H
;save to summation low nibble register
DAA
ADD_L
;SUM0
STA
SUM0,00H
;
ADDH:
LDA
AUG_H,00H
;two number high nibble
;addtition,result
66/126

Ver1.0

ADCM

ADD_H,00H

DAA
STA

ADD_H
SUM1,00H

LDI

TMP,0

ADCM

SUM2,00H

;save to summation middle nibble


;register
;SUM1

;
;addition carry number CY to
;summation
;high nibble register

;
RTNI
;return to main program
;*************************************************************************

67/126

Ver1.0

2-15

BCD 4/8-Bit Subtraction

[Example2-15.1] BCD 4-bit subtraction


;*************************************************************************
;File name: BCDSUB4.ASM
;Description: This program is BCD 4 bit subtraction program ,result high
;
nibble save to SUM0
;Input Arguments: X , Y
;Output Arguments: SUM0 , N
;*************************************************************************
;DATA MEMORY TABLE DEFINE
;******************************
X
EQU
20H
;minuend register
Y
EQU
21H
;subtrahend register
SUM0
EQU
22H
;summation register
N
EQU
23H
;flag registerN=0 is plus ,N=1 is minus
TEMP
EQU
24H
;variable register
;******************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;******************************
RESET:
;____________5-4=1_____________
LDI
X,05H
;set X=05h
LDI
Y,04H
;set Y=04h
CALL
SUBTION
;call subtraction program
;____________2+8=-6____________
LDI
X,02H
;set X=05h
LDI
Y,08H
;set Y=04h
CALL
SUBTION
;call subtraction program
JMP
$
;******************************
; BCD 4 BIT ADDTION SUB PROGRAM
;******************************
SUBTION:
LDI
N,00H
;clear flag N
LDA
Y,00H
SUBM
X,00H
;X-Y
DAS
X
;adjust X
STA
SUM0,00H
;save result to SUM0
BC
ENDSUB
;CY=1 ? yes,goto ENDSUB
CY1:
LDI
N,01H
;no,X<Y,so set N=1
;
LDI
TEMP,0AH
;then SUM0 to do ten complement
LDA
SUM0,00H
SUB
TEMP,00H
STA
SUM0,00H
;
ENDSUB:
RTNI
;return to main program
;*************************************************************************

68/126

Ver1.0

[Example2-15.2] BCD 8-bit subtraction


;*************************************************************************
;File name: BCDSUB8.ASM
;Description: This program is BCD 8 bit subtraction program,result high
;
nibble save to REH ,low nibble save to REL,flag N is show
;
the result is plus or minus
;Input Arguments: MIN_H,MIN_L,SUB_H,SUB_L
;Output Arguments: REH,REL,N
;*************************************************************************
;DATA MEMORY TABLE DEFINE
;******************************
MIN_H
EQU
20H
;minuend high nibble register
MIN_L
EQU
21H
;minuend low nibble register
SUB_H
EQU
22H
;subtrahend high nibble register
SUB_L
EQU
23H
;subtrahend low nibble register
REH
EQU
24H
;result high nibble register
REL
EQU
25H
;result low nibble register
N
EQU
26H
;result flag N=1(plus),n=1(minus)
;******************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;******************************
RESET:
;____________55-44=11__________
LDI
MIN_H,05H
;set minuend =55
LDI
MIN_L,05H
LDI
SUB_H,04H
;set subtrahend =44
LDI
SUB_L,04H
CALL
SUBTION
;call subtraction porgram
;____________44-55=-11_________
LDI
MIN_H,04H
;set minuend =44
LDI
MIN_L,04H
LDI
SUB_H,05H
;set subtrahend =55
LDI
SUB_L,05H
CALL
SUBTION
;call subtraction porgram
JMP
$
;******************************
; BCD 4 BIT ADDTION SUB PROGRAM
;******************************
SUBTION:
LDI
N,00H
;clear flag
LDI
REH,00H
;clear result register REH,REL
LDI
REL,00H
;
SUBL:
LDA
SUB_L,00H
;low nibble subtraction
SUBM
MIN_L,00H
DAS
MIN_L
STA
REL,00H
;save result low nibble to REL
BC
SUBH
SBIM
MIN_H,01H
DAS
MIN_H
;adjust MIN_H
69/126

Ver1.0

;
SUBH:

;
ENDSUB:

BC
LDI

SUBH
N,01H

LDA
SUBM
DAS
STA
BC
LDI
JMP

SUB_H,00H
MIN_H,00H
MIN_H
REH,00H
ENDSUB
N,01H
ENDSUB

LDA
BAZ
LDI
LDI
LDA
SUB
STA
LDA
SUB
STA

;when minuend < subtrahend ,N=1

N,00H
RETSUB
MIN_H,9
MIN_L,10
REH,00H
MIN_H,00H
REH,00H
REL,00H
MIN_L,00H
REL,00H

;high nibble subtraction


;adjust MIN_H
;save result high nibble to REH
;set N=1,result is minus

;if N=1 the result number must be


;to get ten complement number,that is
;(100-result number)

;
RETSUB:
RTNI
;return to main program
;*************************************************************************

70/126

Ver1.0

2-16

BCD 4/8-Bit Multiplication

[Example2-16.1] BCD 4-bit multiplication


;*************************************************************************
;File name: BCDMUL4.ASM
;Description: This program is BCD 4 bit Multiplication program , result
;
high nibble save to RUT_H,low nibble save to RUT_L
;Input Arguments: DEC1,DEC0
;Output Arguments: BIN1, BIN0
;*************************************************************************
;**************************
;DATA MEMORY TABLE DEFINE
;**************************
MUL_A
EQU
20H
;multiplicand register
MUL_B
EQU
21H
;multiplicator register
RUT_H
EQU
22H
;result high nibble register
RUT_L
EQU
23H
;result low nibble register
TEMP
EQU
24H
;variable register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
;__________5 X 4 =20 _______
LDI
MUL_A,5
;set multiplicand =5
LDI
MUL_B,4
;set multiplicator =4
CALL
MUL
;call multiplication program
;__________9 X 9 =81 _______
LDI
MUL_A,9
;set multiplicand =9
LDI
MUL_B,9
;set multiplicator =9
CALL
MUL
;call multiplication program
JMP
$
;**************************
; BCD 4 BIT MULTIPLICATION
;(MUL_A) x (MUL_B) save to
(RUT_H,RUT_L)
;**************************
MUL:
LDI
RUT_H,00H
;clear result high nibble register
LDI
RUT_L,00H
;clear result low nibble register
;
DOMUL:
SBIM
MUL_B,01H
;is MUL_B - 1 >0 ?
BC
$+2
JMP
ENDMUL
;no,goto ENDMUL
LDA
MUL_A,00H
;yes,MUL_A add to RUT_L
ADDM
RUT_L,00H
DAA
RUT_L
LDI
TEMP,00H
;clear AC
ADCM
RUT_H,00h
;add carry to RUL_H
JMP
DOMUL
;goto DOMUL
;
ENDMUL:
RTNI
;return to main program
*************************************************************************
71/126

Ver1.0

[Example2-16.2]BCD 8-bit multiplication


;*************************************************************************
;File name: BCDMUL8.ASM
;Description: This program is BCD 8 bit Multiplication program, result
;
save to RUT_3,RUT_2,RUT_1,RUT_0
;Input Arguments: MUL_A1,MUL_A0,MUL_B1,MUL_B0
;Output Arguments: RUT_3,RUT_2,RUT_1,RUT_0
;*************************************************************************
;DATA MEMORY TABLE DEFINE
;**************************
MUL_A1
EQU
20H
;multiplicand high nibble register
MUL_A0
EQU
21H
;multiplicand low nibble register
MUL_B1
EQU
22H
;multiplicator high nibble register
MUL_B0
EQU
23H
;multiplicator low nibble register
RUT_3
EQU
30H
;result register(thousand)
RUT_2
EQU
31H
;result register(hundred)
RUT_1
EQU
32H
;result register(ten)
RUT_0
EQU
33H
;result register(number)
TEMP
EQU
34H
;variable regiater
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
;__________54 X 54 =2916 ____
LDI
MUL_A1,5
;set multiplicand =54
LDI
MUL_A0,4
LDI
MUL_B1,5
;set multiplicator =54
LDI
MUL_B0,4
CALL
MUL8
;call multiplication program
;_________99 X 99 =9801 _____
LDI
MUL_A1,9
;set multiplicand =99
LDI
MUL_A0,9
LDI
MUL_B1,9
;set multiplicator =99
LDI
MUL_B0,9
CALL
MUL8
;call multiplication program
JMP
$
;**************************
; BCD 8 BIT MULTIPLICATION
;(MUL_A1,MUL_A0) x (MUL_B1,MUL_B0)
;**************************
;save to (RUT_3,RUT_2,RUT_1,RUT_0)
MUL8:
LDI
RUT_3,00H
;clear result register
LDI
RUT_2,00H
LDI
RUT_1,00H
LDI
RUT_0,00H
;
DOMUL:
SBIM
MUL_B0,01H
;if multiplicator - 1 >0,then goto ADDEQS
DAS
MUL_B0
;eles goto ENDMUL
BC
ADDEQS
SBIM
MUL_B1,01H
DAS
MUL_B1
BC
ADDEQS
72/126

Ver1.0

;
ADDEQS:

JMP

ENDMUL

LDA
ADDM
DAA

MUL_A0,00H
RUT_0,00H
RUT_0

;MUL_A0+RUT_0 save to RUT_0


;adjust RUT_0

LDI
ADCM
DAA

TEMP,0
RUT_1,00H
RUT_1

;clear AC =0
;add CY
;adjust AC for add,save to RUT_1,AC

LDI
ADCM
DAA

TEMP,0
RUT_2,00H
RUT_2

;clear AC
;add CY to RUT_2
;adjust RUT_2

LDI
ADCM

TEMP,0
RUT_3,00H

;clear AC
;add CY to RUT_3

LDA
ADDM
DAA

MUL_A1,00H
RUT_1,00H
RUT_1

;RUT_1=MUL_A1+RUT1
;adjust RUT_1

LDI
ADCM
DAA

TEMP,0
RUT_2,00H
RUT_2

;clear AC
;add CY to RUT_2
;adjust RUT_2

LDI
ADCM
JMP

TEMP,0
RUT_3,00H
DOMUL

;clear AC
;add CY to RUT_3
;goto DOMUL

;
ENDMUL:
RTNI
;return to main program
;************************************************************************

73/126

Ver1.0

2-17

BCD 4/8-Bit Division

[Example2-17.1] BCD 4-bit division


;*************************************************************************
;File name: BCDDIV4.ASM
;Description: This program is BCD 4 bit Division program,DIV_A / DIV_B
;
,Quotient save to QUO ,Remainder save to REM
;Input Arguments: DIV_A,DIV_B
;Output Arguments: QUO, REM
;*************************************************************************
;DATA MEMORY TABLE DEFINE
;**************************
DIV_A
EQU
20H
;dividend register
DIV_B
EQU
21H
;divisor register
QUO
EQU
22H
;quotient register
REM
EQU
23H
;remainder register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
;__________9 / 3 = 3 _________
LDI
DIV_A,09H
;set dividend =9
LDI
DIV_B,03H
;set divisor =3
CALL
DIV4
;call division program
;__________2 / 3 = 0 _________
LDI
DIV_A,02H
;set dividend =2
LDI
DIV_B,03H
;set divisor =3
CALL
DIV4
;call division program
JMP
$
;**************************
; BCD 4 BIT DIVISION SUB PROGRAM
;DIV_A / DIV_B
;**************************
DIV4
LDI
QUO,00H
;clear quotient register
;
AGAIN:
LDA
DIV_B,00H
SUBM
DIV_A,00H
;DIV_A - DIV_B
BC
ADD_QUO
;if (DIV_A-DIV_B)>0 then goto
ADD_QUO
JMP
DIV_OK
;
ADD_QUO:
STA
REM,00H
;save remainder to REM
ADIM
QUO,01H
;quotient + 1,save to QUO
JMP
AGAIN
;
DIV_OK:
RTNI
;return to main program
;*************************************************************************

74/126

Ver1.0

[Example2-17.2] BCD 8-bit division


;*************************************************************************
;File name: BCDDIV8.ASM
;Description: This program is BCD 8 bit Division program ,Quotient high
;
nibble save to QUO_H,low nibble save to QUO_L,Remainder
;
high nibble save to REM_H,low nibble save to REM_L
;Input Arguments: DIV_A0,DIV_A1,DIV_B0,DIV_B1
;Output Arguments: QUO_h,QUO_L,REM_H,REM_L
;*************************************************************************
;DATA MEMORY TABLE DEFINE
;**************************
DIV_A1
EQU
20H
;dividend high nibble register
DIV_A0
EQU
21H
;dividend low nibble register
DIV_B1
EQU
22H
;divisor high nibble register
DIV_B0
EQU
23H
;divisor low nibble register
QUO_H
EQU
30H
;quotient high nibble register
QUO_L
EQU
31H
;quotient low nibble register
REM_H
EQU
32H
;remainder high nibble register
REM_L
EQU
33H
;remainder low nibble register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
;_________99 / 10 = 9 _____
LDI
DIV_A1,9
;set dividend = 99
LDI
DIV_A0,9
LDI
DIV_B1,1
;set divisor =10
LDI
DIV_B0,0
CALL
DIV8
;call deivsion program
;_________99 / 2 = 49 ____
LDI
DIV_A1,9
;set dividend = 99
LDI
DIV_A0,9
LDI
DIV_B1,0
;set divisor =2
LDI
DIV_B0,2
CALL
DIV8
;call deivsion program
JMP
$
;***************************
;(DIV_A1,DIV_A0) / (DIV_B1,DIV_B0)
;BCD 8 bit Division program
; =(QUO_H,QUO_L).....(REM_H,REM_L)
;***************************
DIV8:
LDI
QUO_H,00H
;clear QUO_H,QUO_L
LDI
QUO_L,00H
LDA
DIV_A1,0
;save remainder high nibble
STA
REM_H,0
;to REM_H
LDA
DIV_A0,0
;save remainder low nibble
STA
REM_L,0
;to REM_L
DIVH:
LDA
DIV_B1,00H
;(division high nibble)
SUBM
DIV_A1,00H
;DIV_A1 - DIV_B1-->DIV_A1
DAS
DIV_A1
BC
DODIV
JMP
DIV_OK
75/126

Ver1.0

DODIV:

LDA
DIV_B0,00H
;(division low nibble)
SUBM
DIV_A0,00H
;DIV_A0-DIV_B0 --> DIV_A0
DAS
DIV_A0
;adjust AC for sub
BC
ADD_QUO
SBIM
DIV_A1,01H
BC
ADD_QUO
JMP
DIV_OK
ADD_QUO:
LDA
DIV_A1,0
;save remainder high nibble
STA
REM_H,0
;to REM_H
LDA
DIV_A0,0
;save remainder low nibble
STA
REM_L,0
;to REM_L
ADIM
QUO_L,01H
;quotient low nibble + 1,save to
DAA
QUO_L
;QUO_H
LDI
50H,0
;clear AC
ADCM
QUO_H,01H
;quotient high nibble + 1,save to
JMP
DIVH
;QUO_L
DIV_OK:
RTNI
;return to main program
;*************************************************************************

76/126

Ver1.0

2-18

Compare routine

There isnt a direct Compare instruction in SH6610 system, so when you want to do
Compare operation, we can use subtraction and Carry Flag to perform this function. The
following example performs the Compare function in this way.

[Example2-18.1] Compare routine


;*************************************************************************
;File name: COMPARE.ASM
;Description: This program is to compare two number(X,Y) program,if X>Y,set
;
flag N=2; if X=Y,set N=1; if X<Y,set N=3.
;Input Arguments: X , Y
;Output Arguments: N
;*************************************************************************
;DATA MEMORY TABLE DEFINE
;**************************
X
EQU
20H
;variable register
Y
EQU
21H
;variable register
N
EQU
22H
;result register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
LDI
X,06H
LDI
Y,02H
CALL
COMPARE
;return N=2,--> (X > Y)
;__________________________
LDI
X,02H
LDI
Y,06H
CALL
COMPARE
;return N=3,--> (X < Y)
;__________________________
LDI
X,06H
LDI
Y,06H
CALL
COMPARE
;return N=1,--> (X = Y)
JMP
$
;**************************
;COMPARE X , Y
;**************************
COMPARE:
LDA
Y,00H
;get compare data "Y"
SUB
X,00H
;use X-Y to compare two variable
BAZ
SET_N1
;X=Y,set N=1
BC
SET_N2
;X>Y,set N=2
LDI
N,03H
;X<Y,set N=3
JMP
ENDCOM
SET_N1:
LDI
N,01H
JMP
ENDCOM
SET_N2:
LDI
N,02H
;
ENDCOM:
RTNI
;return to main program
;*************************************************************************
77/126

Ver1.0

2-19 Delay
In program designing, a subprogram for delay is often used to do nothing at a given
period. How to calculate the delay time is very important. Suppose an instruction cycle is
2us, then we can delay 2N us by executing N times of instruction cycles.
[Example2-19.1] Subprogram for delay
;*************************************************************************
;File name: DELAY.ASM
;Description: This program is to delay a long time ,you can use parmeter to
;
decide how long to delay.
;Input Arguments: CT2,CT1,CT0,PARM
;*************************************************************************
;DATA MEMORY TABLE DEFINE
;**************************
CT0
EQU
20H
;variable 0 register
CT1
EQU
21H
;variable 1 register
CT2
EQU
22H
;variable 2 register
PARM
EQU
23H
;parmeter register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
;___________delay 12 ms____
LDI
PARM,1
;set parmeter =0
CALL
DELAY
;call delay program
;___________delay 24 ms____
LDI
PARM,2
CALL
DELAY
JMP
$
;**************************
; DELAY 12 x PARM ms
;**************************
DELAY:
SBIM
PARM,01H
;judge parmater is >0
BC
$+2
RTNI
;return to main program
LDI
CT2,0AH
;set counter2=0ah
LDI
CT1,0FH
;set counter1=0fh
LDI
CT0,0FH
;set counter0=0fh
SBIM
CT0,01H
;counter0 down 1
BC
$-1
SBIM
CT1,01H
;counter1 down 1
BC
$-4
SBIM
CT2,01H
;counter2 down 1
BC
$-7
JMP
DELAY
;
;*************************************************************************

78/126

Ver1.0

2-20 Loop
In the following example, we will use Loop to perform three tasks. First: fill in data
memory $80H~$8FH with desired values. Second: add up all the values in data memory
$80H~$8FH. Third: get the largest value from data memory $80H~$8FH.
[Example2-20.1] Loop subprogram
;*************************************************************************
;File name: LOOP.ASM
;Description: This program is three kinds loop program,(fill memory with data
;
routine,sum of data ,maxinum value)
;Input Arguments: DPH,DPM,DPL
;Output Arguments: MAX,SUM1,SUM0
;*************************************************************************
;System register define
;**************************
INX
EQU
0FH
DPL
EQU
10H
DPM
EQU
11H
DPH
EQU
12H
;**************************
;Data register table define
;**************************
TMP
EQU
20H
;vraiable register
MAX
EQU
21H
;max number register
SUM1
EQU
22H
;summation high nibble register
SUM0
EQU
23H
;summation low nibble register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
;-------------------------;CALL LOOP 1
;-------------------------CALL
LOOP1
;-------------------------;CALL LOOP 2
;-------------------------CALL
LOOP2
;-------------------------;CALL LOOP 3
;-------------------------CALL
LOOP3
;-------------------------JMP
$
;**************************
; LOOP 1
; Fill Memory $80h~$8fh with data
;**************************
LOOP1:
LDI
TMP,03H
;set write data =03h
79/126

Ver1.0

LDI
LDI
LDI
LDA
STA
ADIM
BC
JMP
RTNI

DPH,01H
DPM,00H
DPL,00H
TMP,00H
INX,00H
DPL,01H
ENDLP1
$-4

;set index address =80h

;get data
;write data(03h) into data memory
;index address +1
;if index address > $8fh,end fill data

ENDLP1:
;return to main program
;
;**************************
;LOOP 2
;SUM OF DATA form $80h~$8fh
;**************************
LOOP2:
LDI
SUM0,00H
;set high 4 bit =00h
LDI
SUM1,00H
;set low 4 bit =00h
LDI
DPH,01H
;set index address=80h
LDI
DPM,00H
LDI
DPL,00H
LDA
INX,00H
;get data from data memory
ADDM
SUM1,00H
BC
ADDSUM0
ADIM
DPL,01H
;index address +1
BC
ENDLP1
JMP
$-5
ADDSUM0:
ADIM
SUM0,01H
JMP
$-4
ENDLP2:
RTNI
;return to main program
;**************************
;LOOP 3
;GET MAXIMUM VALUE between $80h~$8fh;
;**************************
LOOP3
LDI
MAX,00H
LDI
DPH,01H
;set index address =80h
LDI
DPM,00H
LDI
DPL,00H
LDA
INX,00H
;get data from data memory
SUB
MAX,00H
;compare which number is more
BC
ADD_DPL
;bigger
LDA
INX,00H
;save the max number to MAX
STA
MAX,00H
;register
ADD_DPL:
ADIM
DPL,01H
;index address +1
BC
ENDLP3
;if index address >$8fh,goto ENDLP3
JMP
$-7
ENDLP3:
RTNI
;return to main program
;************************************************************************

80/126

Ver1.0

2-21

Random Number Generator

In this application program, random numbers are generated by the system timer.
[Example2-21.1] Generating four random numbers
;************************************************************************
;File name: RANDOM1.ASM
;Description: This program is use SH6610 timer0 to generate 4 random number
;Input Arguments: TM0,T0L,T0H
;Output Arguments: RAN0,RAN1,RAN2,RAN3
;*************************************************************************
;System register table define
;**************************
INT
EQU
00H
IRQ
EQU
01H
TM0
EQU
02H
T0L
EQU
04H
T0H
EQU
05H
;**************************
;RAM REGISTER TABLE
;**************************
RAN0
EQU
20H
;random 0 register
RAN1
EQU
21H
;random 1 register
RAN2
EQU
22H
;random 2 register
RAN3
EQU
23H
;random 3 register
CNT1
EQU
24H
;variable register
CNT2
EQU
25H
;variable register
;**************************
ORG
00H
JMP
RESET
NOP
JMP
TIMER0
;timer 0 interrupt
NOP
NOP
;**************************
RESET:
LDI
TM0,0101B
;set timer 0 freq.
LDI
T0L,00H
;clear TOL
LDI
T0H,00H
;clear TOH
LDI
INT,04H
;enable timer 0 interrupt
LDI
IRQ,00H
;clear interrupt flag
CALL
RANDOM
;call get random sub_program
JMP
$-1
;**************************
;TIMER 0 INTERRUPT SUB PROGRAM
;**************************
TIMER0
LDI
INX,04H
;enable timer 0 interrupt
LDI
IRQ,00H
;clear interrupt request flag
RTNI
;**************************
;GET FOUR RANDOM NUMBER
;**************************
RANDOM
CALL
DELAY
;call delay time program
LDA
T0H,00H
;read timer 0 high nibble number
STA
RAN0,00H
;save to RAN0
81/126

Ver1.0

LDA
T0L,00H
;read timer 0 low nibble number
STA
RAN1,00H
;save to RAM1
CALL
DELAY
;call delay time program
LDA
T0H,00H
;read timer 0 high nibble number
STA
RAN2,00H
;save to RAN2
LDA
T0L,00H
;read timer 0 low nibble number
STA
RAN3,00H
;save to RAM3
LDA
RAN0,00H
;get random data
STA
T0L,00H
;initial TOL
LDA
RAN3,00H
;get random data
STA
T0H,00H
;initial TOH
RTNI
;****************************
;DELAY FOR A LONG TIME
;****************************
DELAY
;
LDI
CNT1,0AH
LDI
CNT2,0FH
SBIM
CNT2,01H
BC
$-1
SBIM
CNT1,01H
BC
$-4
RTNI
;*************************************************************************

82/126

Ver1.0

2-22

Check sum of data

The main function of Check sum is to check whether certain data has been changed so
as to ensure data validity. For instance, we need to know whether our data has been
damaged after moving or transmission, we can perform Check sum twice, both before and
after moving or transmission, and then compare the two Check sum results. If they are
identical, the data is correct.
In the following example, we will get the check sum of the data in data memory
$80h~$9Fh. Doing logic exclusive or (EOR) and adding up the EOR results of the original
data, we will obtain the check sum value we need.
[Example2-22.1] Check sum of data
;*************************************************************************
;File name: CHKSUM.ASM
;Description: This program is to get check sum from data memory $80h~$9fh,
;
that check sum save to SUM4,SUM3,SUM2,SUM1,SUM0
;Output Arguments: SUM4,SUM3,SUM2,SUM1,SUM0
;*************************************************************************
;SYSTEM REGISTER TABLE DEFINE
;**************************
INX
EQU
0FH
;Pseude index register
DPL
EQU
10H
;Data pointer for INX low nibble
DPM
EQU
11H
;Data pointer for INX middle nibble
DPH
EQU
12H
;Data pointer for INX high nibble
;**************************
;RAM REGISTER TABLE DEFINE
;**************************
TMP
EQU
20H
;variable register
SUM4
EQU
21H
;summation 4 register
SUM3
EQU
22H
;summation 3 register
SUM2
EQU
23H
;summation 2 register
SUM1
EQU
24H
;summation 1 register
SUM0
EQU
25H
;summation 0 register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
CALL
CHKSUM
;call CHKSUM sub program
JMP
$
;**************************
;Check sum of data ($80H-$9FH)
;**************************
CHKSUM:
LDI
SUM4,00H
;set SUM4=0
LDI
SUM3,00H
;set SUM3=0
LDI
SUM2 00H
;set SUM2=0
LDI
SUM1,00H
;set SUM1=0
LDI
SUM0,00H
;set SUM0=0
;-------------------------LDI
DPH,01H
;set index address=$80h
LDI
DPM,00H
83/126

Ver1.0

LDI
DPL,00H
;-------------------------NEXT_DA:
LDA
INX,00H
;get data from data memory
STA
TMP,00H
;save to TMP
;-------------------------EORIM
TMP,0FH
;TMP^0fh -->TMP
ADDM
SUM0,00H
;SUM0+AC-->SUM0
;-------------------------LDI
TMP,00H
;clear AC =0
ADCM
SUM1,00H
;add CY to SUM1
;-------------------------LDI
TMP,00H
;clear AC =0
ADCM
SUM2,00H
;add CY to SUM2
;-------------------------LDI
TMP,00H
;clear AC =0
ADCM
SUM3,00H
;add CY to SUM3
;-------------------------LDI
TMP,00H
;clear AC =0
ADCM
SUM4,00H
;add CY to SUM4
;-------------------------ADIM
DPL,01H
;index address +1
LDI
TMP,00H
;clear AC=0
ADCM
DPM,00H
;if CY =1,index address + 10H
SBI
DPM,02H
;if index address =0AH,then ending
BAZ
ENDCHK
JMP
NEXT_DA
;goto NEXT_DA(to get next data)
ENDCHK:
RTNI
;return to main program
;************************************************************************

84/126

Ver1.0

CHAPTER THREE
System Hardware Application
In the previous chapter, weve learnt about system software application. Now we are
going to introduce the hardware applications of SH6610 system, e.g. timer, system interrupt,
keyboard and I/O port etc.

3-1 Timer
There are two 8-bit counters (up timer 0 and up timer 1) in SH6610 system. Their
starting values can be inputted from outside and the count value can be read out. You
should note that the system counter starts counting immediately after the system is powered
on, so you should pay special attention to presetting its value in your practical use. Timer
can also be used to generate an interrupt, which will be inrtrduced in detail in later chapters.
Some system registers must be set up before using a timer, as listed in the following table:
[Table 3-1.1] Timer controlled register
Address
$02
$03
$04

BIT3 BIT2
------ TM0.2
------ TM1.2

BIT1
TM0.1
TM1.1
T0L

$05

T0H

$06

T1L

$07

T1H

BIT0
TM0.0
TM1.0

R/W
Explanation
R/W Timer 0 mode select register
R/W Timer 1 mode select register
R/W Timer 0 write in/timing register,
Lower 4 bits
R/W Timer 0 write in/timing register,
Higher 4 bits
R/W Timer 1 write in/timing register,
Lower 4 bits
R/W Timer 1 write in/timing register,
Higher 4 bits

85/126

Ver1.0

3-1.1 Timer 0
Timer Mode Register 0 must be selected before using Timer 0. Timer register is used to
select the counters frequency, as listed in the following table:
[Table3-1.1.1] Timer0 frequency table
TM0.2

TM0.1

TM0.0

0
0
0
0
1
1
1
1

0
0
1
1
0
0
1
1

0
1
0
1
0
1
0
1

Divided by
frequency
/2048
/512
/128
/32
/8
/4
/2
/1

Frequency source
OSC/4
OSC/4
OSC/4
OSC/4
OSC/4
OSC/4
OSC/4
OSC/4

After setting up timer mode register, if you want to preset the starting value for the
timer, you can write the desired value into the timer load register, lower 4 bits first and then
higher 4 bits. However, if you read data from the timer, you must read the higher 4 bits first,
and then the lower 4 bits.

[Example3-1.1] Setting up timer 0


TM0
T0L
T0H

EQU
EQU
:
:
LDI
LDI
LDI
:

02H
EQU
05H

;timer 0 mode register


04H
;timer0 load register with lower 4 bits
;timer0 load register with higher 4 bits

TM0,03H
T0L,0AH
T0H,00H

;set timer0 frequency to ((OSC/4)/32)


;set lower 4 bits of timer0s starting value
;set higher 4 bits of timer0s starting value

86/126

Ver1.0

3-1.2 Timer 1
Timer Mode Register (TM1) must be selected before using Timer 1. Timer1 mode
register is used to select the frequency to be inputted to timer, as listed in the following table:
[Table3-1.2.1] Timer1 frequency table
TM1.2
TM1.1
0
0
0
0
1
1
1
1

TM1.0

0
0
1
1
0
0
1
1

0
1
0
1
0
1
0
1

Divided by
frequency
/2048
/512
/128
/32
/8
/4
/2
/1

Frequency source
OSC/4
OSC/4
OSC/4
OSC/4
OSC/4
OSC/4
OSC/4
OSC/4

After setting up timer1 mode register, if you want to preset the starting value for the
timer1, you can write the desired value into the timer1 load register, lower 4 bits first and then
higher 4 bits. However, if you read data from the timer, you must read the higher 4 bits first,
and then the lower 4 bts.
[Example3-1.2] Setting up timer 1
TM1
T1L
T1H

EQU
EQU
EQU
:
:
LDI
LDI
LDI

02H
04H
05H

; timer1 mode register


; timer1 load register with lower 4 bits
; timer1 load register with higher 4 bits

TM1,03H
T1L,0AH
T1H,00H

; set timer1 frequency to ((OSC/4)/32)


; set lower 4 bits of timer1s starting value
; set higher 4 bits of timer1s starting value

3-2 I/O Port Control


SH6610 has 6 groups of I/O ports in total (the number of groups may differ from different
systems, e.g. NT6512 has 2 groups). These 6 groups of I/O pins work by corresponding
registers, so to use SH6610 I/O pins is similar to use to register. To do read/write
instructions to I/O register is equal to do read/write instructions to I/O pins. Corresponding
registers is listed as follows:
[Table3-2.1] I/O port address
Address
BIT0
BIT2
BIT3
$08
PORTA
$09
PORTB
$0A
PORTC
$0B
PORTD
$0C
PORTE
$0D
PORTF

BIT4

R/W
R/W
R/W
R/W
R/W
R/W
R/W

87/126

explanation
I/O port
I/O port
I/O port
I/O port
I/O port
I/O port

Ver1.0

[ Example ] read data from PORT A to AC


instruction format:
LDA

08H,0

[ Example ] write data from AC into PORT A :


instruction format:
STA

08H,0

I/O Port equivalent circuit:

[Chart 3-2.1] CMOS Type

***** TIMER*****
Press MODE key four times
from normal time display,
the unit changes to a timer 00-0000(min,sec,centisecond),
press HR key to start the
timer ,press MIN key to stop the
timer,then press SEC key to clear
[Chart 3-2.2] Bidirection Type
Besides, when I/O pins are used to drive load, it should be noted that SH6610s driving
capability is relatively weak. So other control circuits must be added for loads that require
stronger driving capabilities. SH6610s drive current is 1mA and sink current is 5mA (I/O
driving capability may differ from different systems, and this is the drive capability for
SH66P20).

88/126

Ver1.0

3-3 Interrupt Control


In SH6610 system, there are four interrupt modes: external interrupt, timer0 interrupt,
timer1 interrupt and port interrupt. The system is interrupted only when it is reset or when
interrupt enabled and request flag is 0. The interrupt control bits and interrupt service are
listed as follows:
[Table3-3.1] Table of interrupt control bits
Address BIT3
BIT2
BIT1
BIT0
$00
IEX
IET0
IET1
IEP

$01

IRQ

IRQT0

TM0.1

TM0.1

R/W
Explanation
R/W Interrupt enable flag (external
interrupt, timer0 interrupt, timer1
interrupt and port B, C interrupt)
R/W When interrupt occurs, its
corresponding request bit will be set
to 1

When interrupt occurs, interrupt request flag will be set to 1. If the corresponding
interrupt bit is enabled before, the interrupt will occur in priority order. When interrupt occurs,
PC and CY will be saved in stack, and the value of PC will be replaced by the corresponding
interrupt vector address. In interrupt service program, if you want the system to accept
interrupt signals continuously, then before the program returns with instruction RTNI, you
should clear the interrupt request flag (0) and enable the interrupt enable flag.

Interrupt Flow Chart :

Set up interrupt
service program
address

enable
interruput

if interruput
service
occurs

89/126

go to
interruput
service
program

Ver1.0

3-3.1 External Interrupt (INT *)


The interrupt vector address of external interrupt is $001H. When bit 3(IEX) of
interrupt enable flag is set to 1 and interrupt request flag is 0, external interrupt is enabled. If
an external interrupt signal comes, the system is interrupted (not all systems have external
interrupt).
[Example] Setting up external interrupt
LDI
IEX,1000B
;interrupt enable
LDI
IRQ,0
;clear interrupt request flag

3-3.2 Timer Interrupt


Timer interrupt mainly uses the two timers in the system to trigger interrupt, so theres
two timer interrupts (numbers depending on different systems), timer 0 and timer 1. When
BIT2 (IET0) or BIT1 (IET1) of interrupt enable flag is set to 1, timer interrupt is enabled.
When the value of timer is counted from starting value up to overflow, interrupt occurs (timer
continues counting as long as count frequency is not stopped) and jumps to interrupt service
program. Vector address of timer 0 is $0002H, timer 1 is $0003H. Because the timer of
SH6610 starts counting immediately after powered on, you should pay special attention to its
initial value and the time to enable interrupt
[Example] Setting interrupt for TIMER0
LDI
TM0,3
;set timer 0 freq.(osc/4/32Hz)
LDI
T0L,0
;set timer 0 counter low nibble =0
LDI
T0H,0
;set timer 0 counter high nibble =0
LDI
IEX,0100B
;enable interrupt
LDI
IRQ,0
;clear interrupt request flag
Flow for enabling timer interrupt:
Select timer
frequency
setup Tmode
register

Write in
timer
starting
value

90/126

Enable timer
interrupt

Ver1.0

3-3.3 Port Interrupt


Among system I/O ports, PORT B and PORT C are usually used for triggering interrupt
(port depending on different systems). Bit 0 of interrupt enable flag must be set to 1 so as to
enable Port B, C interrupt. Otherwise they are still normal I/O ports.
However, ports B and C have 8 pins in total, whose interrupt priorities are the same. So
you should be careful when using port interrupt: if one interrupt occurred from any bit (i.e. any
pin of ports B and C changed from high to low), interrupts from other bits are invalid; the
system will not accept another interrupt until the former signal is cleared
We have discussed in previous chapters that control address of I/O ports adopts
memory mapping, so we only need to enable ports b and cs interrupt for interrupts to occur.

3-3.4 Interrupt Priority


When there are two or more interrupts occurring at the same time, their entering order
depends on their respective priorities, as listed in the following table:
Priority
Interrupt Source
-------------------------------------------------------------------1(Highest)
Reset
2
External Interrupt
3
Timer 0
4
Timer 1
5(Lowest)
Port interrupt

3-4 Keyboard Control


3-4.1 Theory of Keyboard
(1) Keyboard circuit:
Most keyboards are arranged their keys in matrix, with horizontal rows and vertical
columns.
To get data inputted through keyboard, the main operation and
consideration is as the following:
a.
b.
c.
d.
e.

Detect if any key is pressed.


Avoiding disturbance from keyboard vibration, i.e. debounce.
How to process multi keys pressed at the same time?
How to process one same key being pressed and not released?
How to convert the obtained key value to corresponding usage code?

(2) Bounce:
When a key is pressed, there must be mechanical vibrations to make the touch point
open and close for many times before a stable touching is achieved, the bouncing time
of being pressed and released varying according to type of switch. If there is only one
switch, generally RS reciprocal switch is used to debounce. If software is used to
process the bouncing of keyboard, just to use a delay period. If special keyboard IC is
used, bouncing circuit is naturally included in IC. The typical procedure is when a key
press is detected, to wait for some time to check again. If the key press still exist, it is
recognized as a valid press. In this way, bouncing will not be recognized as
multi-pressing by continual detection even if CPU speed is very fast. This can also
avoid misrecognition of the instant disturbance signal of the low electric level.
(3)

Multi keys pressed:


91/126

Ver1.0

When a key is pressed while some other key is still not released, there are two general
processing procedures. One is called two-key lockout, i.e. a recognized valid key
press must be released before the system can accept another key press. The other
one is called two-key rollover, i.e. keys pressed at the same time are all recognized as
valid and processed in order.
(4)

Repeated processing: (continuous pressing)

When a key is pressed and not released, there are also two processing procedures.
One is to recognize it as one key press until it is released, no matter how long it has
been pressed. The other is to detect every other period (about 0.5 second) and
recognize as a valid key press.
(5)

Processing time:

In fact, pressing and releasing a key can be seen as two different statuses. Even for
the same key, we can still use program to send different keyboard data on pressing and
on releasing.
The above explanations are for questions you will have when designing keyboard
detection programs. If you are using special keyboard IC, software design can certainly be
simplified. Using I/O port to detect key press makes software complicated, while designing
becomes more freely.

3-4.2 Single button switch key


In the following example, we use PA0 (Port A bit0) to detect key press in a polling way.
Every time a key is pressed, the value of TEMP in register increases by 1, and is displayed in
binary with four LEDs.

[Chart 3-4.2.1] Single button switch key

92/126

Ver1.0

[Example3-4.2.1] Single button switch key


;*************************************************************************
;File name: SINGKEY.ASM
;Description: This program is a single discontinuous key scan program ,
and show the key be hit times to 4 LED display
;Output Arguments: KEY_NUM,TEMP
;*************************************************************************
;SYSTEM REGISTER DEFINE
;**************************
PORT_A
EQU
08H
;I/O port_A define
PORT_B
EQU
09H
;I/O port_B define
;**************************
;DATA MEMORY TABLE DEFINE
;**************************
CT0
EQU
20H
;variable 0 register
CT1
EQU
21H
;variable 1 register
CT2
EQU
22H
;variable 2 register
PARM
EQU
23H
;parmeter register
TEMP
EQU
24H
;variable register
KEY
EQU
25H
;key variable register
KEY_B
EQU
26H
;key variable register
KEY_NUM
EQU
27H
;key number register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
LDI
TEMP,00H
;off LED display
SCAN:
CALL
SCANKEY
;call scankey program
LDA
KEY_NUM,00H ;check key is hit ?
BA0
SHOWLED
;yes,goto show LED
JMP
SCAN
;no,scankey again
SHOWLED:
ADIM
TEMP,01H
;key in times add one
STA
PORT_B,00H
;sent data to PORT_B,to show LED
JMP
SCAN
;**************************
;DISCONTINUOUS SCAN KEY
;**************************
SCANKEY
LDI
PORT_A,0FH
;set PORT_A high
;
LDA
PORT_A,00H
;read key from PORT_A
STA
KEY,00H
;save key to KEY register
;
SBI
KEY,0FH
;if KEY !=0fh,then key is hit
BAZ
CLRKEY
;else jump to CLRKEY
;
LDI
PARM,2
;call delay to debounce
CALL
DELAY
;
LDA
PORT_A,00H
;read key again
SUB
KEY,00H
;if KEY-AC=0,that is the same key
93/126

Ver1.0

;
CHKEY:

CLRKEY:
CLRKB1:
ENDKEY:

BAZ
JMP

CHKEY
CLRKEY

;jump to CHKEY
;else jump to CLRkey

LDA
SUB
BAZ
LDA
STA
EORIM
STA
JMP
LDI
LDI

KEY,00H
KEY_B,00H
CLRKB1
KEY,00H
KEY_B,00H
KEY,0FH
KEY_NUM,00H
ENDKEY
KEY_B,00H
KEY_NUM,00H

;check is the same with last key ?


;if it is the same key ,goto CLRKB1
;yes, goto CLRKB1
;no,it is a new key
;keep key
;get really key number
;save key number to KEY_NUM
;clear KEY_B
;clear KEY_NUM

RTNI
;return to main program
;**************************
; DELAY 12 x PARM ms
;**************************
DELAY:
SBIM
PARM,01H
;judge parameter is >0
BC
$+2
RTNI
;return to main program
LDI
CT2,0AH
LDI
CT1,0FH
LDI
CT0,0FH
SBIM
CT0,01H
BC
$-1
SBIM
CT1,01H
BC
$-4
SBIM
CT2,01H
BC
$-7
JMP
DELAY
;*************************************************************************

94/126

Ver1.0

3-4.3 Continual and non-continual key presses


Continual and non-continual key press procedures are often used in keyboard
application programs. Continual key press procedure: to detect and recognize as a valid
key press every other period (about 0.5 second) while the key is pressed and not released.
Non-continual key press procedure: to recognize as one key press until the key is released,
no matter how long it has been pressed.
In [Example3-4.3.1] we use PA0(Port A bit 0) to detect key press in a polling way.
When a key is pressed and not released, the value of TEMP in register increases by 1 every
other period, and is displayed in binary with four LEDs. For non-continual key press, the
example in last section [Example3-4.2.1] has shown how to process it.
[Example3-4.3.1] Continual key press
;*************************************************************************
;File name: CNKEY.ASM
;Description: This program is a single continual key scan program ,
and show the key be hit times to 4 LED display
;Output Arguments: KEY_NUM,TEMP
;*************************************************************************
;SYSTEM REGISTER DEFINE
;**************************
PORT_A
EQU
08H
;I/O port_A define
PORT_B
EQU
09H
;I/O port_B define
;**************************
;DATA MEMORY TABLE DEFINE
;**************************
AA0
EQU
20H
;variable 0 register
AA1
EQU
21H
;variable 1 register
AA2
EQU
22H
;variable 2 register
PARM
EQU
23H
;parameter register
TEMP
EQU
24H
;variable register
KEY
EQU
25H
;key variable register
KEY_B
EQU
26H
;key variable register
KEY_NUM
EQU
27H
;key number register
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
LDI
TEMP,00H
;clear TEMP
SCAN:
CALL
SCANKEY
;call scankey program
LDA
KEY_NUM,00H ;KEY is hit ?
BA0
SHOWLED
;yes, goto show data to LED
JMP
SCAN
;no, scan again
SHOWLED:
ADIM
TEMP,01H
;add key be hit times
STA
PORT_B,00H
;sent data to PORT_B,to show LED
JMP
SCAN
;**************************
;CONTINUAL SCAN KEY
;**************************
SCANKEY
95/126

Ver1.0

;
CHKEY:

SENTKEY:

;
DELKEY:

;
CLRKEY:

LDI
LDA
STA
SBI
BAZ
LDI
CALL
LDA
SUB
BAZ
JMP

PORT_A,0FH
PORT_A,00H
KEY,00H
KEY,0FH
CLRKEY
PARM,3
DELAY
PORT_A,00H
KEY,00H
CHKEY
CLRKEY

;set PORT_A high


;read key from PORT_A
;save key to KEY register
;check key is hit ?
;no, goto CLRKEY
;yes ,call delay to debounce

LDA
SUB
BAZ
LDA
STA
EORIM
STA
JMP

KEY,00H
KEY_B,00H
DELKEY
KEY,00H
KEY_B,00H
KEY,0FH
KEY_NUM,00H
ENDKEY

;check is the same with last key


;if it is the same key ,goto DELKEY
;yes , goto DELKEY
;no, transfer key number
;keep key
;get really key number
;save key number to KEY_NUM

LDI
CALL
JMP

PARM,0FH
DELAY
SENTKEY

;delay a long time

LDI
LDI

KEY_B,00H
KEY_NUM,00H

;clear KEY_B
;clear KEY_NUM

;read key again


;if KEY-AC=0,that is the same key
;jump to CHKEY
;else jump to CLRkey

ENDKEY:
RTNI
;return to main program
;**************************
; DELAY 12 x PARM ms
;**************************
DELAY:
SBIM
PARM,01H
;judge paramater is >0
BC
$+2
RTNI
;return to main program
LDI
AA2,0AH
LDI
AA1,0FH
LDI
AA0,0FH
SBIM
AA0,01H
BC
$-1
SBIM
AA1,01H
BC
$-4
SBIM
AA2,01H
BC
$-7
JMP
DELAY
;************************************************************************

96/126

Ver1.0

3-4.4 4x4 Matrix Keyboard


In this section, we will discuss how to scan a 4x4 matrix keyboard with SH6610 and
display the key value in binary with 4 LEDs. There are two methods for scanning keyboard.
One is by data table, i.e. first to save each keys corresponding value in program memory,
then adjust obtained signals, and finally read out the values according to addresses
calculated by the program. This method scans one column of keys at one time.
The other method is sequential counting scan. This method scans one key at one
time, and does not need to calculate address of keys value or to read data, because the
relative number is calculated increasingly at every scan and no other process is required.

[Chart 3-4.4.1] 4x4 Keyboard Structure Chart

In [Chart 3-4.4.1], PB0~PB3 send out scan code, and PA0~PA3 receive them. We
can decide which key has been pressed according to scan code and receive code, and
display its value in binary with PC0~PC3 outputting to 4 LEDs.
[Example3-4.4.1] 4X4 Matrix Keyboard Scan
;*************************************************************************
;File name: KEY4X4.ASM
;Description: This program is a 4x4 keyboard scan program ,and show the
;
key number to 4 LED display
;Input Arguments: S_CODE
;Output Arguments: KEY_NUM
;*************************************************************************
;SYSTEM REGISTER DEFINE
;**************************
PORTA
EQU
08H
;I/O port_A define
PORTB
EQU
09H
;I/O port_B define
PORTC
EQU
0AH
;I/O port_C define
TBR
EQU
0EH
;**************************
;DATA MEMORY TABLE DEFINE
;**************************
CT0
EQU
20H
;variable 0 register
97/126

Ver1.0

CT1
EQU
21H
CT2
EQU
22H
PARM
EQU
23H
TEMP
EQU
24H
KY1
EQU
25H
KEY_F
EQU
26H
KEY_NUM
EQU
27H
S_CODE
EQU
28H
B_AC
EQU
29H
B_TBR
EQU
2AH
PORTB_B
EQU
2BH
KY2
EQU
2CH
;**************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;**************************
RESET:
CALL
SCANKEY4
LDA
KEY_F,00H
BAZ
$-2
LDA
KEY_NUM,00H
STA
PORTC,00H
JMP
RESET
;**************************
SCANKEY4
LDI
PORTA,0FH
LDI
PORTB,0FH
LDI
S_CODE,01H
SCAN:
LDA
S_CODE,00H
STA
PORTB_B,00H
EORIM
PORTB_B,0FH
STA
PORTB,00H
LDA
PORTA,00H
STA
KY1,00H
SBI
KY1,0FH
BAZ
NT_CODE
LDI
PARM,3
CALL
DELAY
LDA
PORTA,00H
STA
KY2,00H
SBI
KY2,0FH
BAZ
NT_CODE
LDA
KY1,00H
SUB
KY2,00H
BAZ
PAB1
NT_CODE:
LDA
S_CODE,00H
ADDM
S_CODE,00H
BAZ
R_SAN32
JMP
SCAN
;********************************
;use TABLE to get really key number
;********************************
PAB1:
LDI
B_TBR,0FH
LDI
B_AC,00H
98/126

;variable 1 register
;variable 2 register
;parameter register
;variable register
;key variable register
;key flag register
;key number register
;scan code register
;ac buffer register
;tbr buffer register
;portb buffer register
;key variable register

;call scankey4 program


;if key flag =1,that key is hit
;sent key number to portc to
;show LED (binary 0-F)

;set porta high


;set portb high
;set scan code(first code)

;scan code ^ 0fh


;sent scan code to portb
;read key from porta
;check key is hit ?
;key is hit
;call delay to debounce
;read key from porta again
;check key is hit ?
;key is hit
;check two key is the same ?
;key is really hit
;scan code left 1 bit
;if scan code =0 ,that scan is over
;scan again

Ver1.0

PAB2:

;
PAB3:

LDA
STA
LDA
CALL
SUB
BAZ
JMP

LDA
SUB
BAZ
JMP
PAB4:
LDA
STA
LDI
JMP
ADDAC1:
ADIM
JMP
R_SAN3:
LDI
JMP
R_SAN32:
LDI
LDI
R_SAN31:
RTNI
;**************************
; DELAY 12 x PARM ms
;**************************
DELAY:
SBIM
BC
RTNI
LDI
LDI
LDI
SBIM
BC
SBIM
BC
SBIM
BC
JMP
;
;*********************************
;key number define table
;*********************************
ORG
TJMP
ORG
RTNW
RTNW
RTNW
RTNW
RTNW
RTNW
RTNW
RTNW
RTNW
RTNW
RTNW
RTNW

B_TBR,00H
TBR,00H
B_AC,00H
0700H
KY2,00H
PAB3
ADDAC1
TBR,00H
PORTB_B,00H
PAB4
ADDAC1
B_AC,00H
KEY_NUM,00H
KEY_F,01H
R_SAN31
B_AC,01H
PAB2
KEY_NUM,00H
R_SAN31
KEY_NUM,00H
KEY_F,00H

PARM,01H
$+2

;set TJMP address high nibble


;get TJMP address low nibble
;to get table data
;check table data is the same,
;to scan code

;check scan code is this key

;to save key number


;set key flag =1
;B_AC +1 (data address +1)
;clear key number register
;clear key number register
;clear key number buffer
; return to main program

;judge paramater is >0


;return to main program

CT2,0AH
CT1,0FH
CT0,0FH
CT0,01H
$-1
CT1,01H
$-4
CT2,01H
$-7
DELAY

0700H
07F0H
1110B,1110B
1110B,1101B
1110B,1011B
1110B,0111B
1101B,1110B
1101B,1101B
1101B,1011B
1101B,0111B
1011B,1110B
1011B,1101B
1011B,1011B
1011B,0111B
99/126

;0
;1
;2
;3
;4
;5
;6
;7
;8
;9
;A
;B
Ver1.0

RTNW
0111B,1110B
;C
RTNW
0111B,1101B
;D
RTNW
0111B,1011B
;E
RTNW
0111B,0111B
;F
;*************************************************************************

100/126

Ver1.0

3-5 LED Control


LED is light-emiting diode, which emit light immediately when a voltage above 0.7V is
applied on its two ends. It is usually used for indicating signal and status. Its driving circuit
can be either low level driving or high level driving. Low level driving that LEDs anode (P) is
connected to power source and its cathode is connected to a current limiter and controlled by
I/O pin. When I/O pin outputs 0, the LED lights. High level driving means that LEDs
cathode(N) is connected to GND and its anode(P) is connected to a current limiter and
controlled by I/O pin. When I/O pin outputs 1, the LED lights.

3-5.1 Single LED display


To drive LED usually needs a current of about 10mA. But I/O port of SH6610 has a
limited driving capability. Therefore we must add an electric crystal amplifying circuit as
driving media. The following is a driving circuit for single LED.

[Chart 3-5.1.1] LED Driving Circuit

101/126

Ver1.0

3-5.2 Seven-Segment LED Display


Seven-segment display is composed of 8 LEDs, which are arranged in the shape of 8. It
can be used for indicating number, character and information. According to its internal
connection, there are two types: common-cathode seven-segment display and
common-anode seven-segment display.
Common-cathode seven-segment display connects the cathodes of all LEDs, which
need an input of 1 to light. Its internal structure is shown in [Chart 3-5.2.1]. If you want to
display numbers 0~F, you only need to light LEDs at proper positions. [Table3-5.2.1] is a
character table for 0~F on common-cathode seven-segment display, dp standing for decimal
point.
[Chart 3-5.2.1] Common-cathode seven-segment display

102/126

Ver1.0

[Table3-5.2.1] Character code for common-cathode seven-segment display


dp
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

g
0
0
1
1
1
1
1
0
1
1
1
1
1
1
1
1

f
1
0
0
0
1
1
1
0
1
1
1
1
0
0
1
1

e
1
0
1
0
0
0
1
0
1
0
1
1
1
1
1
1

d
1
0
1
1
0
1
1
1
1
1
0
1
1
1
1
0

c
1
1
0
1
1
1
1
1
1
1
1
1
0
1
0
0

b
1
1
1
1
1
0
0
1
1
1
1
0
0
1
0
0

103/126

a
1
0
1
1
0
1
1
1
1
1
1
0
0
0
1
1

Hex code
3FH
06H
5BH
4FH
66H
6DH
7DH
07H
7FH
6FH
77H
7CH
58H
5EH
79H
71H

Font
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F

Ver1.0

Common-anode seven-segment display connects the anodes of all LEDs, which need an
input of 0 to light. Its internal structure is shown in [Chart 3-5.2.2]. [Table3-5.2.2] is a
character table for 0~F on common-anode seven-segment display.
[Chart 3-5.2.2] Common-anode seven-segment display

[Table3-5.2.2] Character code for common-anode seven-segment display


dp
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1

g
1
1
0
0
0
0
0
1
0
0
0
0
0
0
0
0

f
0
1
1
1
0
0
0
1
0
0
0
0
1
1
0
0

e
0
1
0
1
1
1
0
1
0
1
0
0
0
0
0
0

d
0
1
0
0
1
0
0
0
0
0
1
0
0
0
0
1

c
0
0
1
0
0
0
0
0
0
0
0
0
1
0
1
1

b
0
0
0
0
0
1
1
0
0
0
0
1
1
0
1
1

104/126

a
0
1
0
0
1
0
0
0
0
0
0
1
1
1
0
0

Hex code
C0H
F9H
A4H
B0H
99H
92H
82H
F8H
80H
90H
88H
83H
A7H
A1H
86H
8EH

Font
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F

Ver1.0

3-5.3 LED Decimal display(0~9)


In this section we will make a 0~9 counter with SH6610, and display with
common-cathode seven-segment display. The circuit schematic is shown in [Chart 3-5.3.1].
Character codes of 0~9 of the seven-segment display is outputted through Port A for higher 4
bits and Port B for lower 4 bits. Since the driving capability of SH6610 is relatively weak, an
amplifying circuit is to be connected between the I/O ports and the seven-segment display in
order to enhance driving capability.
[Chart 3-5.3.1] 0~9 Counter circuit

[Example3-5.3.1] 0~9 Counter


;*************************************************************************
;File name: DISLED7.ASM
;Description: This program is use SH6610 to control seven segment display
;
a counter ("0--9")
;Input Arguments: LED_N,N
;Output Arguments: COD_H,COD_L
;*************************************************************************
;SYSTEM REGISTER TABLE DEFINE
;*******************************
PORTA
EQU
08H
PORTB
EQU
09H
TBR
EQU
0EH
;*******************************
;DATA MEMORY TABLE DEFINE
;*******************************
LED_N
EQU
20H
;variable register
COD_H
EQU
21H
;seven segmemt code high nibble
;register
COD_L
EQU
22H
;seven segmemt code low nibble
;register
N
EQU
23H
;N register N=1(anode) N=0(cathode)
AC_B
EQU
24H
;AC buffer register
CT0
EQU
25H
;variable 0 register
CT1
EQU
26H
;variable 1 register
105/126

Ver1.0

CT2
EQU
PARM
EQU
;******************************
ORG
JMP
NOP
NOP
NOP
NOP
;******************************
RESET:
LDI
NT:
LDI

27H
28H

;variable 2 register
;parameter register

00H
RESET

LED_N,00H
N,0

CALL

SEVCODE

LDA
STA

COD_L,00H
PORTA,00H

;set display number


;set N=0,to get cathode seven
;segmemt
;code,then call SEVCODE sub
;program
;sent seven segment low nibble code
;to PORTA

LDA
STA

COD_H,00H
PORTB,00H

;sent seven segment high nibble code


;to PORTB

PARM,0FH
DELAY

;delay a long time

;
LDI
CALL
;
ADIM
LED_N,01H
SBI
LED_N,0AH
BAZ
RESET
JMP
NT
;******************************
;get seven segment display code
;sub program
;******************************
SEVCODE:
LDI
AC_B,01H
LDA
N,00H
STA
TBR,00H
LDA
LED_N,00H
ADDM
AC_B,00H
CALL
0500H
STA
COD_L,00H
LDA
TBR,00H
STA
COD_H,00H
ENDCH:
RTNI
;*******************************
; DELAY 12 x PARM ms
;*******************************
DELAY:
SBIM
PARM,01H
BC
$+2
RTNI
LDI
CT2,0AH
LDI
CT1,0FH
LDI
CT0,0FH
SBIM
CT0,01H
BC
$-1
SBIM
CT1,01H
BC
$-4
SBIM
CT2,01H
106/126

;get next display number


;number =10 ?
;yes,goto RESET
;no, goto NT

;set TJMP address

;get seven segment display code


;save display code low nibble to
;COD_L
;save display code high nibble to
;COD_H

;judge paramater is >0 ?


;no,delay one loop
;yes,return to main program
;set counter number

;counter down

Ver1.0

BC
JMP

$-7
DELAY

;
;*********************************
;seven segment hex caode data area
;*********************************
ORG
0500H
TJMP
;jump by TJMP address
;__________common cathode table__
RTNW
03H,0FH
;0 ;return data (AC=0eh,TBR=07h)
RTNW
00H,06H
;1
RTNW
05H,0BH
;2
RTNW
04H,0FH
;3
RTNW
06H,06H
;4
RTNW
06H,0DH
;5
RTNW
07H,0DH
;6
RTNW
00H,07H
;7
RTNW
07H,0FH
;8
RTNW
06H,0FH
;9
;*************************************************************************

107/126

Ver1.0

3-6 Analog to Digital Conversion Control


Analog to digital converter (shortened form) is used to convert an unknown analog input
signal to its corresponding digital output signal. The analog input signal can be obtained
from sensors output, and the digital output signal from ADC can be used by microprocessor.

3-6.1 Introduction of ADC-0801


In commonly used integrator circuits, ADC0801 series is a kind of gradual analog
converter. It can convert analog input voltage of 0 ~ +5V to an equivalent of 8-bit binary
number. It only requires a power supply of +5V and has a pulse generator inside its chip.
Chart 3-6.1.1 shows the pins of ADC0801.
[Chart 3-6.1.1] Chart of ADC0801 pins

108/126

Ver1.0

The following is a function explanation of ADC0801 series pins.


1.CS (pin1)
Chip selecting line, low level is available
2.RD (pin2)
Controlling pin for external circuits to read the converter results. Only when CS and RD
are both at low level, converted data can be appearred on output pins.
3.WR (pin3)
Controlling pin (input) to inform the converter to start up conversion. To start up
conversion, CS must be at low level. When WR is changing to low level, the converter
clears itself first and starts the conversion after WR returns to low level.
4.CLK IN (pin4)
Input pin for external clock. When clock is inputted from external, the frequency shall not
exceed 640K Hz.
5.CLK R (pin19)
This pin is the inverse output pin for CLK IN. System clock inside ADC0801 series is
outputted through this pin.
6.INTR (pin5)
Signal pin to inform the external circuits that the converter has finished conversion. INTR
is at high level when the conversion starts, and falls to low level when it ends.
7.VIN(+-) (pins 6, 7)
Input pins for difference analog signals. When positive voltage is inputted from single-pin,
pin 7 is to be connected to GND and signal comes in through pin 6; when negative voltage
is inputted from single-pin, the connection is just the other way round.
8.VREF/2 (pin9)
In ADC0801 series, VREF is the maximum analog input voltage. When using this pin, just
input half of reference voltage (VREF/2) to this pin. If this pin is floating, reference voltage
equals the power source VCC.
When external voltage is inputted as reference voltage, the input voltage must not exceed
half of VCC, i.e. the maximum value of VREF shall not exceed VCC.
9.A GND (pin8)
Analog ground.
10.D GND(pin10)
Digital ground.
11.VCC(pin20)
Input pin for external power supply, also can serve as input pin for reference voltage
(VREF). Usually we use +5V power source.
12.DB0 to DB7(pin18 to pin11)
Input pins for digital data. They are tri-state input pins and can be connected directly to
common data line.

109/126

Ver1.0

If external circuit sets /CS=0, it means to use ADC converter. Then /WR input pin
changes from high level to low level, data in converter register is cleared and in the meantime,
INTR pin changes to high level and /WR returns to high level from low. At this time the
converter starts to convert data. When all data has been converted, /INTR is automatically
set to low level, indicating the conversion is finished. Then the external circuit can set /RD
pin to low level and can successfully read digital data out. Finally /CS is set to high level
again, the whole conversion is completed.
Theres no signal control pins which can connect directly to surrounding IC in NT6XXX
series system, so when NT6XXX system is to be used for controlling conversion from analog
to digital, its I/O must be simulated to /WR and /RD pins. To use NT6XXX I/O ports as /WR
and /RD pins, we must write software program to simulate control signals of conversion. In
this way we can use NY6XXX to successfully control ADC0801 for conversion.
3-6.2.1 ADC0801 Function Experiment
This experiment is to convert analog voltage to digital signal. We will use ADC0801 to
convert analog voltage to digital signal. When ADC0801 converts external analog voltage
signal to digital, /INTR goes from high to low, where the program can decide that the
conversion has finished and send the digital signal outputted from ADC0801 to LED display
for us to observe the converted voltage digital value.
[Chart 3-6.2.1] ADC0801 application circuit chart

110/126

Ver1.0

[Example3-6.2.1] ADC0801 application program


;***********************************************************************
;File name: ADC.ASM
;Description: This program is Analog to Digital conversion program,
;
we use NT66P22 to contron ADC0801,and output Digital
;
to eight LED display from PORTD,PORTE.
;Input Arguments: PORTA,PORTB,PORTC
;Output Arguments: PORTD,PORTE
;************************************************************************
;SYSTEM REGISTER TABLE DEFINE
;**************************
CTL_PORT
EQU
08H
;<<port a>>
;
;bit 0: /CS
;
;bit 1: /RD
;
;bit 2: /WR
;
;bit 3: /INTR
D_PORT_L
EQU
09H
;<<port b>>
;
;data port low nibble
D_PORT_H
EQU
0AH
;<<port c>>
;
;data port high nibble
O_PORT_L
EQU
0BH
;<<port d>>
;
;out data port low nibble
O_PORT_H
EQU
0CH
;<<port e>>
;
;out data port high nibble
PAOUT
EQU
16H
;set portA to be output port
PBOUT
EQU
17H
;set portB to be output port
PCOUT
EQU
18H
;set portC to be output port
PDOUT
EQU
19H
;set portD to be output port
PEOUT
EQU
1AH
;set portE to be output port
;*************************
ORG
00H
JMP
RESET
NOP
NOP
;timer 0 interrupt
NOP
NOP
;**************************
RESET:
LDI
CTL_PORT,0FH
LDI
PAOUT,0FH
;set portA to be output port
LDI
PBOUT,00H
;set portB to be input port
LDI
PCOUT,00H
;set portC to be input port
LDI
PDOUT,0FH
;set portD to be output port
LDI
PEOUT,0FH
;set portE to be output port
;
AGAIN:
LDI
CTL_PORT,0EH
;enable ADC0801 (set /CS=0)
LDI
CTL_PORT,0AH
;set /WR=0,start to convert
LDI
CTL_PORT,0EH
;set /INTR=1,/WR=1
;
LDA
CTL_PORT,00H
;if /INTR=0,then convert ok
BA3
$-1
;else wait it converturn ok
;
LDI
CTL_PORT,0CH
;set /CS=0,/RD=0 to get Digital
;
; data
LDA
D_PORT_L,00H
;read data low nibble from PORT_B
STA
O_PORT_L,00H
;out data to PORT_D for LED
111/126

Ver1.0

; display
;read data high nibble from
; PORT_C
STA
O_PORT_H,00H
;out data to PORT_E for
JMP
AGAIN
; LED display
;********************************************************************************
LDA

D_PORT_H,00H

112/126

Ver1.0

3-7 PWM Control Application


In this section we will introduce to you how to use timer (Timer 0 or Timer 1) in
NT66P22 to produce PWM pulse wave signals when the system clock is 2M Hz. And then
we will use these signals, together with external circuit (integrator), to form a 4-bit
digital/analog converter(DAC), and use PWM signals of different duty cycles together with
external circuit (integrator) to produce sine signals.

3-7.1 D/A using PWM


TEMP is planned to have 4 bits in the program (TEMP value 0~15), therefore a
complete unit wave output has 16 interrupts. So to produce PWM signals of 1KHz, the time
of each interrupt is (1/1k)/16=64us. The unit cycle frequency can be changed when the
values of T0M, T0L and T0H are changed in the program.
Here we use the timer 0 of NT66P22 for time base unit for pulse wave width. In the
program the timer 0 is set to produce an interrupt at an interval of 64us (i.e. TM0=04H,
T0L=0CH, T0H=0FH). This interval will be used as the time unit for pulse output. The
program uses a 4-bit value to control duty cycle of pulse output through PA0. When TEMP
is set to 1, its duty cycle is 1/16. When TEMP value grows, the duty cycle grows together,
and so is the width of output pulse.
Since the interrupt service program takes 14us to 20us to execute, and the basic timing unit
is at least 64us, execution of the interrupt service program will not affect normal operation of
the main program.
After receipt of PWM signal, we add this signal to an integrator circuit, as shown in
[Chart 3-7.1.1]. A voltage can be obtained when the PWM signal goes through the
integrator circuit, thus making a 4-bit digital/analog converter. The voltage production
formula is: VO=(TEMP) x (5V/16)

[Chart 3-7.1.1] PWM circuit chart

113/126

Ver1.0

[Example3-7.1.1] D/A using PWM


;************************************************************************
;File name: NTPWM.ASM
;Description: This program is use NT66P22 to generate PWM,than use PWM
;
to convert Digital to Analog ,like a Digital to Analog
;
conversion.(D/A using PWM)
;Input Arguments: TEMP,CNT1
;Output Arguments: PORT_A
;*************************************************************************
;SYSTEM REGISTER DEFINE
;**************************
IEX
EQU
00H
;interrupt enable register
IRQ
EQU
01H
;interrupt request flags
TM0
EQU
02H
;Timer 0 mode register
T0L
EQU
04H
;Timer 0 load/counter register low
;digit
T0H
EQU
05H
;Timer 0 load/counter register high
;digit
PORT_A
EQU
08H
;I/O PORT_A
PAOUT
EQU
16H
;portA setup register
;**************************
;DATA MEMORY TABLE DEFINE
;**************************
TEMP
EQU
21H
;duty cycle register
CNT1
EQU
22H
;duty cycle counter register
AC_B
EQU
23H
;buffer register
;**************************
ORG
00H
JMP
RESET
NOP
JMP
TIMER0
NOP
NOP
;**************************
RESET:
LDI
PAOUT,0FH
LDI
TM0,0011B
LDI
T0L,0FH
LDI
T0H,0FH
LDI
IEX,0100B
LDI
IRQ,00H
;***************************
LDI
TEMP,01H
LDI
CNT1,00H
JMP
$
;**************************
;TIMER 0 IEXERRUPT SUB PROGRAM
;**************************
TIMER0
STA
AC_B,00H
LDI
IRQ,00H
LDI
IEX,0100B
LDA
TEMP,00h
BAZ
$+4
ADIM
CNT1,01H
114/126

;set portA to be output port


;set timer 0 freq.(OSC/4)/32=64 us
;set T0L=0FH
;set T0H=0FH
;enable timer 0 interrupt
;clear interrupt flag
;set TEMP=1 (duty cycle =1/16)
;clear CNT1

;push AC
;clear interrupt flag
;enable Timer 0 interrupt

;check duty cycle is over?


Ver1.0

SUB
TEMP,00H
;if (TEMP-CNT1) > 0,then out"1"to
BC
$+3
;PORT_A,else out"0"to PORT_A
LDI
PORT_A,00H
JMP
$+2
LDI
PORT_A,01H
LDA
AC_B,00H
;pop AC
RTNI
;return to main program
;*************************************************************************

115/126

Ver1.0

3-7.2 Sine Wave Producer


In this section we will use NT66P22 to produce PWM signals of different duty cycles
(1/16T ~15/16T, T=1k Hz), which we will then send (in order of 1/16T, 2/16T, .. .. .. .
15/16T14/16T, 13/16T, .. .. .. 1/16T) to an integrator circuit (as shown in [Chart 3-7.2.1])
through PA0 continuously. PWM will produce 30 different electric levels and phases by the
integrator circuit. If we observe these 30 electric levels with an oscillograph, they appear as
a sine wave, and thus achieve the sine wave we want. Of course the unit cycles frequency
can be changed when the values of T0M, T0L and T0H are changed in the program.
[Chart 3-7.2.1] Sine Wave Producer circuit

116/126

Ver1.0

[Example3-7.2.1] Sine Wave Producer


;*************************************************************************
;File name: SINE.ASM
;Description: This program is Sine Wave generation we use NT66P22 to
;
generate PWM,then use different PWM sent to D/A circuit
;
,we can get one sine wave out from D/A circuit.
;Input Arguments: TEMP,CNT1,CNT2
;Output Arguments: PORT_A
;*************************************************************************
;SYSTEM REGISTER DEFINE
;******************************
IEX
EQU
00H
;interrupt enable register
IRQ
EQU
01H
;interrupt request flags
TM0
EQU
02H
;Timer 0 mode register
T0L
EQU
04H
;Timer 0 load/counter register low
;digit
T0H
EQU
05H
;Timer 0 load/counter register high
;digit
PORT_A
EQU
08H
;I/O PORT_A
PAOUT
EQU
16H
;portA setup register
;******************************
;Data Memory TABLE Define
;******************************
TEMP
EQU
20H
;duty cyclr register
CNT1
EQU
21H
;duty cycle counter register
CNT2
EQU
22H
;variable register
;******************************
ORG
00H
JMP
RESET
NOP
JMP
TIMER0
;goto TIMER0
NOP
NOP
;******************************
RESET:
LDI
PAOUT,0FH
;set portA to be output port
LDI
TM0,0011B
;set timer 0 freq.(OSC/4)/32=64 us
LDI
T0L,0FH
;set T0L=0FH
LDI
T0H,0FH
;set T0H=0FH
LDI
IEX,0100B
;enable timer 0 interrupt
LDI
IRQ,00H
;clear interrupt flag
;*******************************
LDI
TEMP,00H
;clear TEMP
LDI
CNT1,00H
;set CNT1=0
LOOP:
JMP
LOOP
;goto LOOP
;*******************************
;TIMER 0 INTERRUPT SUB PROGRAM
;*******************************
TIMER0
LDI
IRQ,00H
;clear interrupt flag
LDI
IEX,0100B
;enable tomer 0 interrupt
;*******************************
;PWM generation freq.=1k Hz
;resolution=1/16
117/126

Ver1.0

;*******************************
ADIM
CNT1,01H
BC
CNG
SUB
TEMP,00H
BC
$+3
LDI
PORT_A,00H
;set PORT_A =0
JMP
$+2
LDI
PORT_A,01H
;set port_B =1
JMP
RT1
;*******************************
;to generate next duty cycle
;*******************************
CNG
LDA
CNT2,00H
;if CNT2 != 0 ,then goto UP
BAZ
UP
;------------------------------;to get next duty cycle
; (15/16T-->1/16T)
;------------------------------SBIM
TEMP,01H
;get next duty cycle
BC
RT1
LDI
CNT2,00H
;set CNT2=0
LDI
TEMP,00H
;set TEMP=0
JMP
RT1
;-----------------------------;to get next duty cycle
; (1/16T-->15/16T)
;------------------------------UP:
ADIM
TEMP,01H
;get next duty cucle
BC
$+2
JMP
RT1
LDI
CNT2,01H
;set CNT2=1
LDI
TEMP,0FH
;set TEMP=0FH
RT1:
RTNI
;return to main program
;*************************************************************************

118/126

Ver1.0

3-8 Music Control (PSG)


In this section we will discuss the music world of NT6XXX with the IC NT6510 as an
example on how to design great music with system functions.

[Chart 3-8.1.1] NT6510 PSG block chart

***** TIMER*****
Press MODE key four times from normal time display,
the unit changes to a timer 00-00-00(min,sec,centisecond),
press HR key to start the timer ,press MIN key to stop the
timer,then press SEC key to clear the timer(00-00-00)
Press MODE key again to switch back to normal time

3-8.1 Theory of PSG


PSG(programmable sound generator), there are two channels in NT6510 system,
Channel 1 being a 7-bit (C1.0 ~ C1.6) pseudo random timer, Channel 2 being a 15-bit (C2.0
~ C2.14 ) pseudo random counter. They have two modes: one is Tone mode and the other is
Noise mode. You can switch from one mode to the other with the bit C1M of channel 1:
when C1M=0, channel 1 is in tone mode; when C1M=1, channel 1 is in noise mode. For
channel 2, switching is controlled by the bit C2M: when C2M=0, channel 2 is in tone mode;
when C2M=1, channel 2 is in noise mode.
Volume of PSG is controlled by the bits VOL1 and VOL0, which can be in one of the
four modes, 100%, 75%, 25% or 0%, as shown in [Table3-8.1.1].

[Table3-8.1.1] PSG volume


VOL1
1
1
0
0

VOL2
1
0
1
0

VOL
100%
75%
25%
0%

You must disable PSG when the system is entering STOP or HALT mode. Its related
control bits are show in [Table3-8.1.2].

119/126

Ver1.0

[Table3-8.1.2] Table of PSG control bits


Address
bit 3
bit 2
bit 1
C1.3
C1.2
C1.1
$13
C1M
C1.6
C1.5
$14
C2.3
C2.2
C2.1
$15
C2.7
C2.6
C2.5
$16
C2.11
C2.10
C2.9
$17
C2M
C2.14
C2.13
$18
VOL1
VOL0 CH2EN
$19

$1A
$1B

P1.1
P2.1

bit 0
C1.0
C1.4
C2.0
C2.4
C2.8
C2.12
CH1EN

P1.0
P2.0

R/W
W
W
W
W
W
W
R/W

R/W
R/W

Remarks
PSG channel 1 low digit
PSG channel 1 high digit
PSG channel 2 low digit
PSG channel 2
PSG channel 2
PSG channel 2 high digit
bit 0:PSG channel 1 enable
bit 1:PSG channel 2 enable
bit 2,bit 3:Volume Control
(initial is 0,no sound)
PSG 1 Prescaler
PSG 2 Prescaler

Prescaler is another very important controller for PSG control. Its clock source is a
clock of 32KHz frequency, and it divide the setup value to obtain the required frequency, as
shown in [Table3-8.1.3]:
[Table3-8.1.3] Prescaler table
P.1
P.0
Prescaler Divide Ratio
0
0
1
0
1
2
1
0
4
1
1
8

Clock Source
32k Hz
32k Hz
32k Hz
32k Hz

Actual Clock
32k Hz
16k Hz
8k Hz
4k Hz

3-8.2 Music Player Program


In this section, we will introduce to you how to play great music with NY6510 system and
how to write music player program. Of course, before entering the world of program writing,
we must first know the musical scales NT6510 can control. All of them are list for you in
[Table3-9.2.1]:

120/126

Ver1.0

[Table3-8.2.1] NT6510 musical scale table


Key
Ideal Freq.
Divide No.
C3
D3
E3
F3
C3#
D3#
F3#
G3
A3
B3
C4
D4
E4
F4
G4
A4
B4
C5
D5
E5
F5
G5
G3#
A3#
C4#
D4#
F4#
G4#
A4#
C5#
D5#
F5#

130.813
146.832
164.816
174.614
138.591
155.563
185.000
196.000
220.000
246.942
261.626
293.655
329.628
349.288
391.955
440.000
493.883
523.251
587.330
659.255
698.456
783.991
207.652
233.082
277.183
311.127
369.994
415.305
466.164
554.364
622.254
739.989

122
109
97
92
115
103
86
82
73
65
61
54
49
46
41
36
32
31
27
24
23
20
77
69
58
51
43
39
34
29
26
22

Tone Freq.
131.148
146.789
164.948
173.913
139.130
155.340
186.046
195.122
219.178
246.154
262.295
296.296
326.530
347.826
390.243
444.444
500.000
526.129
592.592
666.666
695.652
800.000
207.792
231.884
275.862
313.725
327.093
410.256
470.588
551.724
615.384
727.272

121/126

0x13,
0x17
0
1
5
3
1
9
A
7
1
4
9
A
B
E
8
A
5
B
B
C
9
C
A
C
D
6
6
3
9
E
7
3

0x14,
0x18
2
5
4
3
6
7
6
2
2
4
4
5
5
5
5
1
2
4
3
5
3
4
7
1
4
5
7
6
6
2
7
7

Error
0.25
0.03
0.08
0.4
0.39
0.14
0.5
0.4
0.3
0.3
0.2
0.9
0.9
0.4
0.4
1.0
1.2
1.3
0.8
1.1
0.4
2.0
0.06
0.5
0.5
0.8
0.6
1.2
0.9
0.5
1.1
1.7

Ver1.0

[Example3-8.2.1] Playing a piece of music


In this example, we use the NT6510 system to play a piece of music (repeating the
cycle from mediant DO to alt DO). Here we use channel 1 to put music, set maximum
volume and set prescaler to be 32k Hz.
;*************************************************************************
;File name: SOUND.ASM
;Description: This program is a music program ,we use NT6510 system to
;
play a simple music
;*************************************************************************
;SYSTEM REGISTER TABLE DEFINE
;*******************************
PSG1_H
EQU
13H
;PSG Channel 1 low digit
PSG1_L
EQU
14H
;PSG Channel 1 high digit
PSG_CTL
EQU
19H
;PSG enable ,volume control
PSG1PSC
EQU
1AH
;PSG 1 prescaler
TBR
EQU
0EH
;table branch register
;*******************************
;DATA MEMORY TABLE DEFINE
;*******************************
MUS_N
EQU
20H
;sound number register
FREQ_L
EQU
21H
;FREQ. low register
FREQ_H
EQU
22H
;FREQ. high register
AC_B
EQU
23H
;ac buffer register
PARM
EQU
24H
;delay paremeter register
AA0
EQU
25H
;delay counter0 register
AA1
EQU
26H
;delay counter1 register
AA2
EQU
27H
;delay counter2 register
;******************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
;******************************
RESET:
;******************************
;INITION SOUND REGISTER
;******************************
LDI
PSG_CTL,0DH
;set channel 1 enable,volume max
LDI
PSG1PSC,00H
;set channel 1 prescaler =32k Hz
;******************************
;PLAY SOUND "DO--DO^"
;******************************
LDI
MUS_N,00H
;set sound number ,play "Do"
CALL
GETCODE
;call GETCODE program
LDA
FREQ_L,00H
STA
PSG1_L,00H
;sent FREQ. to channel 1 low digit
LDA
FREQ_H,00H
STA
PSG1_H,00H
;sent FREQ. to channel 1 digh digit
LDI
PARM,0FH
CALL
DELAY
;play a long time
ADIM
MUS_N,01H
;get next sound number
SBI
MUS_N,08H
;if sound number =8,then play again
BAZ
$-10
;else play next sound
122/126

Ver1.0

JMP
$-10
;play music again
;******************************
;get music code
;******************************
GETCODE:
LDI
AC_B,01H
;set AC ,TBR
LDI
TBR,00H
LDA
MUS_N,00H
ADDM
AC_B,00H
CALL
0500H
;call TJMP to get music freq. code
STA
FREQ_L,00H
;code low nibble save to FREQ_L
LDA
TBR,00H
STA
FREQ_H,00H
;code high nibble save to FREQ_H
ENDCH:
RTNI
;*******************************
; DELAY 12 x PARM ms
;*******************************
DELAY:
SBIM
PARM,01H
;judge paramater is >0
BC
$+2
RTNI
;return to main program
LDI
AA2,0AH
LDI
AA1,0FH
LDI
AA0,0FH
SBIM
AA0,01H
BC
$-1
SBIM
AA1,01H
BC
$-4
SBIM
AA2,01H
BC
$-7
JMP
DELAY
;
;*********************************
;seven segment hex caode data area
;*********************************
ORG
0500H
TJMP
;__________MUSIC TABLE___________
RTNW
09H,04H
;Do ;return data (AC=04h,TBR=09h)
RTNW
0AH,05H
;Re
RTNW
0BH,05H
;Me
RTNW
0EH,05H
;Fa
RTNW
08H,05H
;So
RTNW
0AH,01H
;La
RTNW
05H,02H
;Si
RTNW
0BH,04H
;DO^
;*************************************************************************

123/126

Ver1.0

[Example3-8.2.2] Simulating the sound of a train


In this example we use the NT6510 system to simulate the sound of a train. The
program uses the noise function of PSG to simulate a trains start voice, walking voice and
Bu Bu voice.
;*************************************************************************
;File name: TRAIN.ASM
;Description: This program is use NT6510 system to play music,we use
;
Noise to get like a train start voice ,walking voice,
;
and Bu Bu voice
;*************************************************************************
;DATA MEMORY TABLE DEFINE
;**************************
COUNT01
EQU
22H
;counter register
COUNT02
EQU
23H
;counter register
COUNT03
EQU
24H
;counter register
COUNT11
EQU
32H
;counter register
COUNT22
EQU
33H
;counter register
COUNT33
EQU
34H
;counter register
FLAG
EQU
25H
;variable register
FLAG1
EQU
26H
;variable register
FLAG2
EQU
28H
;variable register
;*************************
ORG
00H
JMP
RESET
NOP
NOP
NOP
NOP
RESET:
LDI
FLAG1,00H
;run counter==0
LDI
FLAG,0FH
;setup speed_slag=0fh
LDI
FLAG2,04H
;
AGAIN:
CALL
CHE_CHE
;send "che che" voice
CALL
DELAY
LDI
19H,00H
;close voice
CALL
DELAY
CALL
CHA_CHA
;send "cha cha" voice
CALL
DELAY
LDI
19H,00H
;close voice
CALL
DELAY
SBIM
FLAG,01H
;compare speed_flag==0
BAZ
REPET
;if speed=0 then jmp to repet
SBI
FLAG,02H
;speed sub two level
BAZ
RET
JMP
AGAIN
RET:
SBIM
FLAG2,01H
BAZ
AGAIN
LDI
FLAG,02H
;let speed stay at level 2 four times
JMP
AGAIN
REPET:
LDI
FLAG,01H
ADIM
FLAG1,01H
;Add run counter
SBI
FLAG1,0FH
;If run counter=0fh then send "bu bu"
voice
BAZ
BU
124/126

Ver1.0

JMP

AGAIN

CALL

BU_BU

LDI
CALL
SBIM
BC
LDI
JMP

TMP,0FH
DELAY
TMP,01H
$-2
FLAG1,00H
AGAIN

;delay long time

19H,0FH
1AH,0001B
1BH,0001B
14H,0AH
13H,00H
18H,0AH
17H,00H

;open chanl & voice max.


;set chanl 1 actual clock =16k hz
;set chanl 2 actual clock =16k hz
;send freq. to chanl 1

BU:

;Send "bu bu" voice

;
;*****************************
CHE_CHE
;
LDI
LDI
LDI
LDI
LDI
LDI
LDI
RTNI
;****************************
CHA_CHA
LDI
LDI
LDI
;
LDI
LDI
;
LDI
LDI
RTNI
;*********************************
BU_BU
LDI
LDI
LDI
LDI
LDI
LDI
LDI
RTNI
;**********************************;
;DELAY TIME SUB PROGRAM
;**********************************;
DELAY
LDA
STA
DELAY3:
LDI
DELAY2:
LDI
DELAY1:
SBIM
BC
CALL
SBIM
BC
SBIM

;send rreq. to chanl 2


;return to main program

19H,0FH
1AH,0010B
1BH,0010B

;open chanl & voice max.


;set chanl 1 actual clock =8k hz
;set chanl 2 actual clock =8k hz

14H,0AH
13H,00H

;send freq. to chanl 1

18H,0AH
17H,00H

;send freq. to chanl 2

1AH,00H
1BH,00H
13H,0CH
14H,04H
19H,0FH
17H,0CH
18H,0CH

;set chanl 1 actual clock =32k hz


;set chanl 2 actual clock =32k hz
;send freq. to chanl 1
;open chanl & voice max.
;send freq. to chanl 2

FLAG,00H
COUNT03,00H
COUNT01,01H
COUNT02,08H
COUNT02,01H
DELAY1
DELAY_1
COUNT01,01H
DELAY2
COUNT03,01H
125/126

Ver1.0

BC
DELAY3
RTNI
;**********************************;
;DELAY TIME SUB PROGRAM
;**********************************;
DELAY_1
LDI
COUNT33,0DH
DELAY33
LDI
COUNT11,0FH
DELAY22
LDI
COUNT22,0FH
DELAY11
SBIM
COUNT22,01H
BC
DELAY11
SBIM
COUNT11,01H
BC
DELAY22
SBIM
COUNT33,01H
BC
DELAY33
RTNI
;*************************************************************************

126/126

Ver1.0

You might also like