Creating a new instruction for Sx-chip
Beside studying how each instruction works step-by-step in
microprogram, one will understand better by "doing" the thing. To get
more understanding how a control unit of a processor really work, we
show how to create a new instruction for Sx-chip. "decrement and jump if not zero" is
a useful instruction. It exists in most processors. It is used to
repeat some code section for a fixed amount. Here is an example:
repeat
10
s = s + 1
We will use k to store 10 and decrement it until zero.
lit.10
:loop
put.k
inc.s
get.k
djnz.loop
The value to be decrement is on the top of stack. Here is the
microstep:
:djnz
alu(ts,dec)->ts
The first step is to decrement ts. Next is to test if ts is zero, if it
is then not-jump. ts is pop first to discard the count.
alu(ts=0) ifTrue djnz2
:djnz2
mR(sp)->ts
sp-1, pc+1, fetch
For jump case, the microstep is similar to jump instruction.
pc+arg, fetch
These steps when translated into the microprogram language:
:djnz
x.ts alu.dec b.tbus lts ;
x.ts alu.z ifT /djnz2 ;
j.pcarg lpc /fetch ;
:djnz2
x.sp alu.p1 a.tbus mR
b.dbus lts ;
x.sp alu.dec lsp j.pc1 lpc
/fetch ;
Hand-on
The microstep for "djnz" is incorporated into "mspec.txt" file.
The first step is to generate the microprogram (bit). Using "mgen" (in
dir sx0/mpgm). Keep the output in "mpgm.txt". "mspec.h" will also be
generated. It contains all signal bindings to be used by the
Sx-simulator.
c:>sx0\mpgm\mgen > mpgm.txt
The next step is to generate "sxbit.h" file using "sxgen" (in dir
sx0/sxgen). sxgen requires two files as input: mspec.h and
mpgm.txt. We have to define the new code for "djnz" first, in the
header file "sx.h".
#define
icDjnz 39
The new binding for microprogram address of the "djnz" instruction is
added to the function "initrom()" in sxgen (sx.c). Then recompile the
sxgen.
void
initrom(void){
udop[icAdd] =
lookup("bop");
...
udop[icDjnz] =
lookup("djnz");
}
Now use sxgen to generate sxbit:
c:>sx0\sxgen\sxgen
The Sx-simulator (sx0) must be recompiled with this new
"sxbit.h". We use this source file to test the new
instruction. The
assembler is modified to accept "djnz". (My tool is not
yet automatic. So, low level change is still necessary,
unfortunately)
;
test djnz
s 2
k 1
#
call.main
sys.13
:main
fun.3
lit.0
put.s ; s = 0
lit.10
:loop
put.k ; initially k = 10
inc.s
get.k
djnz.loop
get.s
sys.1 ; print s
ret.3
#
When this program is run, the output to the screen (the trace of
addresses of the program in execution) looks like:
C:\prabhas\bag\sx0\test>sx0
a.obj
load program, last address 14
DP 1
1 4 5 6 7 8 9 10 7 8 9 10
7 8 9 10 7 8 9 10 7 8 9 10
7 8 9 10 7 8 9 10 7 8 9 10
7 8 9 10 7 8 9 10 11 12
<10>13 2
Enjoy
last update 4 July 2011