SlideShare a Scribd company logo
1 of 23
1
Digital Systems and Microprocessor Design
(H7068)
Daniel Roggen
Richmond 3A13
d.roggen@sussex.ac.uk
9. Exercises /
Laboratory
2014
2
Circuit complexity
CPU with custom instructions
3
Objective / success criteria
• Prerequisite: lab of week 8, knowledge of digital
electronics
• The objective of this laboratory is to:
– Understand how the ALU of the educational processor is
implemented
– Understand how to add instructions to the educational processor
(specifically the ALU)
• Success criteria:
– You have implemented the two custom instructions in the tutorial
of this lab
– You have implemented the random number generator for the
coursework assignment
4
Custom instructions
• The easiest place to add custom instructions is in the
ALU
– The CPU has been designed to have an extensible ALU
– The instruction decoding to feed the ALU with data and store the
result is already in place!
• Modifying or creating instructions unrelated to the ALU
may involve significant changes to the control unit and
other parts of the processor
– For instance creating an instruction that takes 3 parameters
instead of 2 would require to change the register bank
– Creating an instruction that moves from memory to memory
would require more than 3 clock cycles per instruction...
5
Custom ALU instruction
• ALU instructions are have opcodes: 001, 010, 011
• The ALU data path is activated for these opcodes
• The general format of the ALU instruction is:
• The specific ALU operation is defined by the opcode and
the ALUop bits in the instruction: instruction(12..10)
• Instruction(12) is called R'/I, but it can have a different
meaning.
– Single operand ALU instructions use R'/I as part of ALUop
instruction(15..8) Instruction(7..0)
opcode ALU op dreg src
0 X X X X X r r i/
-
i/
-
i/
-
i/
-
i/
-
i/
-
i/
r
i/
r
IR /
6
Default ALU data path
• For ALU instructions, the control unit does the following:
– Input A of the ALU receives the data from register dreg
– Input B of the ALU receives either the immediate src or the value
of the register src, depending on instruction(12)
– The output of the ALU is stored in register dreg
– The ALU receives instruction(14..10) to indicate the operation to
perform.
• Providing data to the ALU and storing the result is done
automatically by the control unit
• We simply need to add new function to the ALU - the
rest is taken care of!
instruction(15..8) Instruction(7..0)
opcode ALU op dreg src
0 X X X X X r r i/
-
i/
-
i/
-
i/
-
i/
-
i/
-
i/
r
i/
r
IR /
7
Default ALU data path
• The only part of the processor we look at is the ALU in
cpualu.vhd
• It looks like this:
A B
instruction(14..10)
to dst register
from dst register from src register or immediate
8
Tutorial I
• The instruction table shows that the processor has only 3
2-input logic functions: AND, OR, XOR
• The objective is to add a new logic instruction: NAND
• The ALU is defined in cpualu.vhd
9
Tutorial I
• Step 1: define which instruction code corresponds to the
new function
– The opcode has to be 001, 010, 011 (ALU operation)
– Check the instruction table to find which opcode has "space" to
contain a new instruction
– Opcode 001 is full: all combinations instruction(12..10) are used
– Opcode 010 is not full: only 4 combinations of instruction(12..10)
are used for the xor and cmp. This could be used.
– Opcode 011 is not full: only 5 combinations of instruction(12..10)
are used. This could be used.
– To keep some logic in the instruction set we decide to use
opcode 010, as the existing instructions with opcode 010 are
two-operand instructions (xor and cmp)
– However nothing prevents us from using opcode 011!
10
Tutorial I
• Step 2: Decide which ALUop to use for the selected
opcode
– ALUop 00 is xor
– ALUop 01 is cmp
– Choose a value of ALUop for this instruction. Let's say 10!
– We can update our instruction table:
Custom
instruction
opcode ALU op dreg immedite / reg
nand r, r 0 1 0 0 1 0 r r - - - - - - r r
nand r, i 0 1 0 1 1 0 r r i i i i i i i i
IR / IR /
11
Tutorial I
• Step 3: Prepare to modify cpualu.vhd
– The ALU is realized by a multiplexer selecting one function:
r <= a+b when op(4 downto 3)="01" and op(1 downto 0)="00" else
sub(7 downto 0) when op(4 downto 3)="01" and op(1 downto 0)="01" else
a and b when op(4 downto 3)="01" and op(1 downto 0)="10" else
a or b when op(4 downto 3)="01" and op(1 downto 0)="11" else
a xor b when op(4 downto 3)="10" and op(1 downto 0)="00" else
not a when op(4 downto 0)="11000" else
'0'&a(7 downto 1) when op(4 downto 0)="11001" else
a(0)&a(7 downto 1) when op(4 downto 0)="11010" else
a(7)&a(7 downto 1) when op(4 downto 0)="11011" else
a(6 downto 0)&a(7) when op(4 downto 0)="11100" else
"00000000";
– We must add an input to this multiplexer to get the NAND of
inputs a and b when the opcode is 010 and ALUop is 10
– The current ALU uses op(4..0) to select the operation.
– Where does op come from? Read cpu.vhd to find out!
– op is actually instruction(14..10)
12
Tutorial I
• Step 4: Modify cpualu.vhd
– We want to execute the NAND when instruction(15..10) is
"010010"..... (is this correct??!)
– So op(4..0)="10010" for a NAND
– Thus modify the ALU as follows:
r <= a+b when op(4 downto 3)="01" and op(1 downto 0)="00" else
sub(7 downto 0) when op(4 downto 3)="01" and op(1 downto 0)="01" else
a and b when op(4 downto 3)="01" and op(1 downto 0)="10" else
a or b when op(4 downto 3)="01" and op(1 downto 0)="11" else
a xor b when op(4 downto 3)="10" and op(1 downto 0)="00" else
a nand b when op(4 downto 0)="10010" else
not a when op(4 downto 0)="11000" else
'0'&a(7 downto 1) when op(4 downto 0)="11001" else
a(0)&a(7 downto 1) when op(4 downto 0)="11010" else
a(7)&a(7 downto 1) when op(4 downto 0)="11011" else
a(6 downto 0)&a(7) when op(4 downto 0)="11100" else
"00000000";
13
Tutorial I
• Step 5: create a test program
– We want to test the register mode and the immediate mode!
– We use the instruction table to find the encoding
-- register mode
mov ra,a5h 10a5
mov rb,aah 11aa
nand ra,rb 4801
-- expected result: 5F
mov rc,83h 1283
nand rc,84h 5a84
-- expected result: 7F
Custom ALU
instruction
opcode ALU op dreg immedite / reg
nand r, r 0 1 0 0 1 0 r r - - - - - - r r
nand r, i 0 1 0 1 1 0 r r i i i i i i i i
IR /
14
Tutorial I
• Step 6: test the program
– Edit the memory to fill it with the instructions:
Address Data
00 10
01 A5
02 11
03 AA
04 48
05 01
06 12
07 83
08 5A
09 84
15
Tutorial I
• Step 7: fix bugs
– The program works with register source, but not with immediate
source!
– The error is in this line in the multiplexer in cpualu.vhd:
a nand b when op(4 downto 0)="10010" else
– Can you fix this?
16
Tutorial II
• The previous instruction was combinational. We will now
see how to make an ALU instruction whose internal
function is clock dependent
• Let's say we want an instruction "tick" that returns the
number of clock cycles since the processor was turned
on.
• We will implement this with a circuit counting up at each
clock cycle, with a synchronous reset
• The counter output will be connected to one input of the
ALU multiplexer and the "tick" instruction will select that
input
17
Tutorial II
• Step 1: define which instruction code corresponds to the
new function
– As before the opcode has to be 001, 010, 011 (ALU operation),
and there is space in the opcodes 010 and 011
– To keep some logic in the instruction set we use opcode 011 this
time: all instructions with this opcode have a single operand, as
does "tick"
• Step 2: Decide which ALUop to use for the selected
opcode
– ALUop 000 is not, 001 is shr, 010 is ror, 011 is asr, 100 is rol.
– Let's choose the next free ALUop: 101
– We can update our instruction table:
Instructions instruction(15..8) Instruction(7..0)
Custom ALU
instructions
opcode ALU op dreg
tick 0 1 1 1 0 1 r r - - - - - - - -
18
Tutorial II
• Step 3: Create the tick counter: a counter with reset
– We could create a dedicated VHDL component (in a separate
file), but we are lazy here and put everything in cpualu.vhd
– Add a signal in cpualu.vhd: tick as an 8-bit std_logic_vector
– Add the following counter code somewhere in the architecture:
• We use a handy VHDL operation for the addition!
• This is a D flip-flop with synchronous reset and an increment
process(clk)
begin
if rising_edge(clk) then
if rst='1' then
tick<="00000000";
else
tick<=tick+1;
end if;
end if;
end process;
19
Tutorial II
• Step 4: Modify the multiplexer
– Select "tick", the output of the counter, when instruction(14..10)
is "11101"
r <= a+b when op(4 downto 3)="01" and op(1 downto 0)="00" else
sub(7 downto 0) when op(4 downto 3)="01" and op(1 downto 0)="01" else
a and b when op(4 downto 3)="01" and op(1 downto 0)="10" else
a or b when op(4 downto 3)="01" and op(1 downto 0)="11" else
a xor b when op(4 downto 3)="10" and op(1 downto 0)="00" else
not a when op(4 downto 0)="11000" else
'0'&a(7 downto 1) when op(4 downto 0)="11001" else
a(0)&a(7 downto 1) when op(4 downto 0)="11010" else
a(7)&a(7 downto 1) when op(4 downto 0)="11011" else
a(6 downto 0)&a(7) when op(4 downto 0)="11100" else
tick when op(4 downto 0)="11101" else
"00000000";
20
Tutorial II
• Step 5: Create a test program.
– We will create a loop with register RB that goes from 0 to 4
– We will put the number of clock cycles to complete the look in d
(we can verify later that this is correct!)
– Then the program continuously loads c with the tick
0 mov rb,00 1100
2 add rb,1 3101
4 cmp rb,4 5504
6 jb 2 BB02
8 tick rd 7700
A tick rc 7600
C jmp A B00A
21
Tutorial II
• Step 6: Fill in the memory, reset the CPU and test the
program
– What do you obtain in register C eventually?
– Is this what you expect from the program? Verify this by counting
the number of instructions executed in the program! Hint: the
program loops 4 times the instructions between address 2 and
6....
– You may find that your count is off by one cycle... can you
explain this?
22
Problem 1: Random Number Generator
(coursework assignment)
• Creating a pseudo-random number can be realized with a linear
feedback shift register:
• The 8-bit number represented by bits r7..r0 is pseudo-random
• In reality the sequence is periodical
• If the taps of the XOR are well chosen (as in this circuit) the
periodicity can be of maximal length (here 255 clocks)
• Note that this circuit must be initialized properly: upon reset, at least
one bit must be one. Let's say r7=1 on reset
23
Problem 1: Random Number Generator
(coursework assignment)
• The objective is to add an instruction to the ALU that
returns a random number using the LFSR circuit and
demonstrate that it works with a test program
• You are free to select any unused instruction encoding
• Check the coursework assignment for details on what
you must include in the report.
• Hint: you can create a standalone VHDL module to test
the LFSR independently of the processor, and then
instantiate the circuit in the ALU

More Related Content

What's hot

An introduction to ROP
An introduction to ROPAn introduction to ROP
An introduction to ROPSaumil Shah
 
8051 microcontroller
8051 microcontroller8051 microcontroller
8051 microcontrollerjokersclown57
 
Temperature Sensor with LED matrix Display BY ►iRFAN QADOOS◄ 9
Temperature Sensor with LED matrix Display BY ►iRFAN QADOOS◄ 9Temperature Sensor with LED matrix Display BY ►iRFAN QADOOS◄ 9
Temperature Sensor with LED matrix Display BY ►iRFAN QADOOS◄ 9Irfan Qadoos
 
Temperature sensor with a led matrix display (arduino controlled)
Temperature sensor with a led matrix display (arduino controlled)Temperature sensor with a led matrix display (arduino controlled)
Temperature sensor with a led matrix display (arduino controlled)TechLeap
 
Arduino Foundations
Arduino FoundationsArduino Foundations
Arduino FoundationsJohn Breslin
 
8051 training an interactive tutorial
8051 training an interactive tutorial8051 training an interactive tutorial
8051 training an interactive tutorialFutura infotech
 
Arduino: Analog I/O
Arduino: Analog I/OArduino: Analog I/O
Arduino: Analog I/OJune-Hao Hou
 
Using Ready-for-PIC and SDR Libraries
Using Ready-for-PIC and SDR LibrariesUsing Ready-for-PIC and SDR Libraries
Using Ready-for-PIC and SDR LibrariesCorrado Santoro
 
Subroutine in 8051 microcontroller
Subroutine in 8051 microcontrollerSubroutine in 8051 microcontroller
Subroutine in 8051 microcontrollerbhadresh savani
 
1347 Assembly Language Programming Of 8051
1347 Assembly Language Programming Of 80511347 Assembly Language Programming Of 8051
1347 Assembly Language Programming Of 8051techbed
 
Mao arduino
Mao arduinoMao arduino
Mao arduinoMao Wu
 

What's hot (20)

An introduction to ROP
An introduction to ROPAn introduction to ROP
An introduction to ROP
 
8051 microcontroller
8051 microcontroller8051 microcontroller
8051 microcontroller
 
Doc2502
Doc2502Doc2502
Doc2502
 
Plc operation part 3
Plc operation part 3Plc operation part 3
Plc operation part 3
 
Temperature Sensor with LED matrix Display BY ►iRFAN QADOOS◄ 9
Temperature Sensor with LED matrix Display BY ►iRFAN QADOOS◄ 9Temperature Sensor with LED matrix Display BY ►iRFAN QADOOS◄ 9
Temperature Sensor with LED matrix Display BY ►iRFAN QADOOS◄ 9
 
Temperature sensor with a led matrix display (arduino controlled)
Temperature sensor with a led matrix display (arduino controlled)Temperature sensor with a led matrix display (arduino controlled)
Temperature sensor with a led matrix display (arduino controlled)
 
2502s
2502s2502s
2502s
 
Arduino Foundations
Arduino FoundationsArduino Foundations
Arduino Foundations
 
89c5131datasheet
89c5131datasheet89c5131datasheet
89c5131datasheet
 
8051 training an interactive tutorial
8051 training an interactive tutorial8051 training an interactive tutorial
8051 training an interactive tutorial
 
Arduino: Analog I/O
Arduino: Analog I/OArduino: Analog I/O
Arduino: Analog I/O
 
I o ports.ppt
I o ports.pptI o ports.ppt
I o ports.ppt
 
Using Ready-for-PIC and SDR Libraries
Using Ready-for-PIC and SDR LibrariesUsing Ready-for-PIC and SDR Libraries
Using Ready-for-PIC and SDR Libraries
 
Pir code
Pir codePir code
Pir code
 
Subroutine in 8051 microcontroller
Subroutine in 8051 microcontrollerSubroutine in 8051 microcontroller
Subroutine in 8051 microcontroller
 
1347 Assembly Language Programming Of 8051
1347 Assembly Language Programming Of 80511347 Assembly Language Programming Of 8051
1347 Assembly Language Programming Of 8051
 
ECE 467 Final Project
ECE 467 Final Project ECE 467 Final Project
ECE 467 Final Project
 
Ii avr-basics(1)
Ii avr-basics(1)Ii avr-basics(1)
Ii avr-basics(1)
 
Lecture 2 timers, pwm, state machine IN PIC
Lecture 2   timers, pwm, state machine IN PIC Lecture 2   timers, pwm, state machine IN PIC
Lecture 2 timers, pwm, state machine IN PIC
 
Mao arduino
Mao arduinoMao arduino
Mao arduino
 

Similar to Digital Systems CPU Custom Instructions Lab

(a)Suppose the main memory of the Pep8 were completely filled with .docx
(a)Suppose the main memory of the Pep8 were completely filled with .docx(a)Suppose the main memory of the Pep8 were completely filled with .docx
(a)Suppose the main memory of the Pep8 were completely filled with .docxajoy21
 
PLC: Principios básicos del controlador lógico programable mediante el softwa...
PLC: Principios básicos del controlador lógico programable mediante el softwa...PLC: Principios básicos del controlador lógico programable mediante el softwa...
PLC: Principios básicos del controlador lógico programable mediante el softwa...SANTIAGO PABLO ALBERTO
 
W8_1: Intro to UoS Educational Processor
W8_1: Intro to UoS Educational ProcessorW8_1: Intro to UoS Educational Processor
W8_1: Intro to UoS Educational ProcessorDaniel Roggen
 
Advanced Techniques for Exploiting ILP
Advanced Techniques for Exploiting ILPAdvanced Techniques for Exploiting ILP
Advanced Techniques for Exploiting ILPA B Shinde
 
Alu design-project
Alu design-projectAlu design-project
Alu design-projectalphankg1
 
UNIT 3 - General Purpose Processors
UNIT 3 - General Purpose ProcessorsUNIT 3 - General Purpose Processors
UNIT 3 - General Purpose ProcessorsButtaRajasekhar2
 
ADS Lab 5 Report
ADS Lab 5 ReportADS Lab 5 Report
ADS Lab 5 ReportRiddhi Shah
 
Loop Unroll_ACA_CS505.ppt
Loop Unroll_ACA_CS505.pptLoop Unroll_ACA_CS505.ppt
Loop Unroll_ACA_CS505.pptHassanJavaid48
 
Embedded system - Introduction To ARM Exception Handling and Software Interru...
Embedded system - Introduction To ARM Exception Handling andSoftware Interru...Embedded system - Introduction To ARM Exception Handling andSoftware Interru...
Embedded system - Introduction To ARM Exception Handling and Software Interru...Vibrant Technologies & Computers
 
Micro controller(pratheesh)
Micro controller(pratheesh)Micro controller(pratheesh)
Micro controller(pratheesh)Pratheesh Pala
 
Algorithm-RepetitionSentinellNestedLoop_Solution.pptx
Algorithm-RepetitionSentinellNestedLoop_Solution.pptxAlgorithm-RepetitionSentinellNestedLoop_Solution.pptx
Algorithm-RepetitionSentinellNestedLoop_Solution.pptxAliaaAqilah3
 
Micro c lab3(ssd)
Micro c lab3(ssd)Micro c lab3(ssd)
Micro c lab3(ssd)Mashood
 
ESD_05_ARM_Instructions set for preparation
ESD_05_ARM_Instructions set for preparationESD_05_ARM_Instructions set for preparation
ESD_05_ARM_Instructions set for preparationssuser026e94
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 

Similar to Digital Systems CPU Custom Instructions Lab (20)

(a)Suppose the main memory of the Pep8 were completely filled with .docx
(a)Suppose the main memory of the Pep8 were completely filled with .docx(a)Suppose the main memory of the Pep8 were completely filled with .docx
(a)Suppose the main memory of the Pep8 were completely filled with .docx
 
W10: Interrupts
W10: InterruptsW10: Interrupts
W10: Interrupts
 
PLC: Principios básicos del controlador lógico programable mediante el softwa...
PLC: Principios básicos del controlador lógico programable mediante el softwa...PLC: Principios básicos del controlador lógico programable mediante el softwa...
PLC: Principios básicos del controlador lógico programable mediante el softwa...
 
W8_1: Intro to UoS Educational Processor
W8_1: Intro to UoS Educational ProcessorW8_1: Intro to UoS Educational Processor
W8_1: Intro to UoS Educational Processor
 
ARMInst.ppt
ARMInst.pptARMInst.ppt
ARMInst.ppt
 
ARMInst.ppt
ARMInst.pptARMInst.ppt
ARMInst.ppt
 
W8: Laboratory 1
W8: Laboratory 1W8: Laboratory 1
W8: Laboratory 1
 
Advanced Techniques for Exploiting ILP
Advanced Techniques for Exploiting ILPAdvanced Techniques for Exploiting ILP
Advanced Techniques for Exploiting ILP
 
Alu design-project
Alu design-projectAlu design-project
Alu design-project
 
UNIT 3 - General Purpose Processors
UNIT 3 - General Purpose ProcessorsUNIT 3 - General Purpose Processors
UNIT 3 - General Purpose Processors
 
ADS Lab 5 Report
ADS Lab 5 ReportADS Lab 5 Report
ADS Lab 5 Report
 
Loop Unroll_ACA_CS505.ppt
Loop Unroll_ACA_CS505.pptLoop Unroll_ACA_CS505.ppt
Loop Unroll_ACA_CS505.ppt
 
Embedded system - Introduction To ARM Exception Handling and Software Interru...
Embedded system - Introduction To ARM Exception Handling andSoftware Interru...Embedded system - Introduction To ARM Exception Handling andSoftware Interru...
Embedded system - Introduction To ARM Exception Handling and Software Interru...
 
Micro controller(pratheesh)
Micro controller(pratheesh)Micro controller(pratheesh)
Micro controller(pratheesh)
 
Algorithm-RepetitionSentinellNestedLoop_Solution.pptx
Algorithm-RepetitionSentinellNestedLoop_Solution.pptxAlgorithm-RepetitionSentinellNestedLoop_Solution.pptx
Algorithm-RepetitionSentinellNestedLoop_Solution.pptx
 
Micro c lab3(ssd)
Micro c lab3(ssd)Micro c lab3(ssd)
Micro c lab3(ssd)
 
12 mt06ped008
12 mt06ped008 12 mt06ped008
12 mt06ped008
 
Ch5_MorrisMano.pptx
Ch5_MorrisMano.pptxCh5_MorrisMano.pptx
Ch5_MorrisMano.pptx
 
ESD_05_ARM_Instructions set for preparation
ESD_05_ARM_Instructions set for preparationESD_05_ARM_Instructions set for preparation
ESD_05_ARM_Instructions set for preparation
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 

More from Daniel Roggen

Embedded Sensing and Computational Behaviour Science
Embedded Sensing and Computational Behaviour ScienceEmbedded Sensing and Computational Behaviour Science
Embedded Sensing and Computational Behaviour ScienceDaniel Roggen
 
W9_3: UoS Educational Processor: Assembler Memory Access
W9_3: UoS Educational Processor: Assembler Memory AccessW9_3: UoS Educational Processor: Assembler Memory Access
W9_3: UoS Educational Processor: Assembler Memory AccessDaniel Roggen
 
W9_2: Jumps and loops
W9_2: Jumps and loopsW9_2: Jumps and loops
W9_2: Jumps and loopsDaniel Roggen
 
W8_2: Inside the UoS Educational Processor
W8_2: Inside the UoS Educational ProcessorW8_2: Inside the UoS Educational Processor
W8_2: Inside the UoS Educational ProcessorDaniel Roggen
 
Wearable technologies: what's brewing in the lab?
Wearable technologies: what's brewing in the lab?Wearable technologies: what's brewing in the lab?
Wearable technologies: what's brewing in the lab?Daniel Roggen
 
Wearable Computing - Part IV: Ensemble classifiers & Insight into ongoing res...
Wearable Computing - Part IV: Ensemble classifiers & Insight into ongoing res...Wearable Computing - Part IV: Ensemble classifiers & Insight into ongoing res...
Wearable Computing - Part IV: Ensemble classifiers & Insight into ongoing res...Daniel Roggen
 
Wearable Computing - Part III: The Activity Recognition Chain (ARC)
Wearable Computing - Part III: The Activity Recognition Chain (ARC)Wearable Computing - Part III: The Activity Recognition Chain (ARC)
Wearable Computing - Part III: The Activity Recognition Chain (ARC)Daniel Roggen
 
Wearable Computing - Part II: Sensors
Wearable Computing - Part II: SensorsWearable Computing - Part II: Sensors
Wearable Computing - Part II: SensorsDaniel Roggen
 
Wearable Computing - Part I: What is Wearable Computing?
Wearable Computing - Part I: What is Wearable Computing?Wearable Computing - Part I: What is Wearable Computing?
Wearable Computing - Part I: What is Wearable Computing?Daniel Roggen
 

More from Daniel Roggen (10)

Embedded Sensing and Computational Behaviour Science
Embedded Sensing and Computational Behaviour ScienceEmbedded Sensing and Computational Behaviour Science
Embedded Sensing and Computational Behaviour Science
 
W10: Laboratory
W10: LaboratoryW10: Laboratory
W10: Laboratory
 
W9_3: UoS Educational Processor: Assembler Memory Access
W9_3: UoS Educational Processor: Assembler Memory AccessW9_3: UoS Educational Processor: Assembler Memory Access
W9_3: UoS Educational Processor: Assembler Memory Access
 
W9_2: Jumps and loops
W9_2: Jumps and loopsW9_2: Jumps and loops
W9_2: Jumps and loops
 
W8_2: Inside the UoS Educational Processor
W8_2: Inside the UoS Educational ProcessorW8_2: Inside the UoS Educational Processor
W8_2: Inside the UoS Educational Processor
 
Wearable technologies: what's brewing in the lab?
Wearable technologies: what's brewing in the lab?Wearable technologies: what's brewing in the lab?
Wearable technologies: what's brewing in the lab?
 
Wearable Computing - Part IV: Ensemble classifiers & Insight into ongoing res...
Wearable Computing - Part IV: Ensemble classifiers & Insight into ongoing res...Wearable Computing - Part IV: Ensemble classifiers & Insight into ongoing res...
Wearable Computing - Part IV: Ensemble classifiers & Insight into ongoing res...
 
Wearable Computing - Part III: The Activity Recognition Chain (ARC)
Wearable Computing - Part III: The Activity Recognition Chain (ARC)Wearable Computing - Part III: The Activity Recognition Chain (ARC)
Wearable Computing - Part III: The Activity Recognition Chain (ARC)
 
Wearable Computing - Part II: Sensors
Wearable Computing - Part II: SensorsWearable Computing - Part II: Sensors
Wearable Computing - Part II: Sensors
 
Wearable Computing - Part I: What is Wearable Computing?
Wearable Computing - Part I: What is Wearable Computing?Wearable Computing - Part I: What is Wearable Computing?
Wearable Computing - Part I: What is Wearable Computing?
 

Recently uploaded

Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 

Recently uploaded (20)

Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 

Digital Systems CPU Custom Instructions Lab

  • 1. 1 Digital Systems and Microprocessor Design (H7068) Daniel Roggen Richmond 3A13 d.roggen@sussex.ac.uk 9. Exercises / Laboratory 2014
  • 2. 2 Circuit complexity CPU with custom instructions
  • 3. 3 Objective / success criteria • Prerequisite: lab of week 8, knowledge of digital electronics • The objective of this laboratory is to: – Understand how the ALU of the educational processor is implemented – Understand how to add instructions to the educational processor (specifically the ALU) • Success criteria: – You have implemented the two custom instructions in the tutorial of this lab – You have implemented the random number generator for the coursework assignment
  • 4. 4 Custom instructions • The easiest place to add custom instructions is in the ALU – The CPU has been designed to have an extensible ALU – The instruction decoding to feed the ALU with data and store the result is already in place! • Modifying or creating instructions unrelated to the ALU may involve significant changes to the control unit and other parts of the processor – For instance creating an instruction that takes 3 parameters instead of 2 would require to change the register bank – Creating an instruction that moves from memory to memory would require more than 3 clock cycles per instruction...
  • 5. 5 Custom ALU instruction • ALU instructions are have opcodes: 001, 010, 011 • The ALU data path is activated for these opcodes • The general format of the ALU instruction is: • The specific ALU operation is defined by the opcode and the ALUop bits in the instruction: instruction(12..10) • Instruction(12) is called R'/I, but it can have a different meaning. – Single operand ALU instructions use R'/I as part of ALUop instruction(15..8) Instruction(7..0) opcode ALU op dreg src 0 X X X X X r r i/ - i/ - i/ - i/ - i/ - i/ - i/ r i/ r IR /
  • 6. 6 Default ALU data path • For ALU instructions, the control unit does the following: – Input A of the ALU receives the data from register dreg – Input B of the ALU receives either the immediate src or the value of the register src, depending on instruction(12) – The output of the ALU is stored in register dreg – The ALU receives instruction(14..10) to indicate the operation to perform. • Providing data to the ALU and storing the result is done automatically by the control unit • We simply need to add new function to the ALU - the rest is taken care of! instruction(15..8) Instruction(7..0) opcode ALU op dreg src 0 X X X X X r r i/ - i/ - i/ - i/ - i/ - i/ - i/ r i/ r IR /
  • 7. 7 Default ALU data path • The only part of the processor we look at is the ALU in cpualu.vhd • It looks like this: A B instruction(14..10) to dst register from dst register from src register or immediate
  • 8. 8 Tutorial I • The instruction table shows that the processor has only 3 2-input logic functions: AND, OR, XOR • The objective is to add a new logic instruction: NAND • The ALU is defined in cpualu.vhd
  • 9. 9 Tutorial I • Step 1: define which instruction code corresponds to the new function – The opcode has to be 001, 010, 011 (ALU operation) – Check the instruction table to find which opcode has "space" to contain a new instruction – Opcode 001 is full: all combinations instruction(12..10) are used – Opcode 010 is not full: only 4 combinations of instruction(12..10) are used for the xor and cmp. This could be used. – Opcode 011 is not full: only 5 combinations of instruction(12..10) are used. This could be used. – To keep some logic in the instruction set we decide to use opcode 010, as the existing instructions with opcode 010 are two-operand instructions (xor and cmp) – However nothing prevents us from using opcode 011!
  • 10. 10 Tutorial I • Step 2: Decide which ALUop to use for the selected opcode – ALUop 00 is xor – ALUop 01 is cmp – Choose a value of ALUop for this instruction. Let's say 10! – We can update our instruction table: Custom instruction opcode ALU op dreg immedite / reg nand r, r 0 1 0 0 1 0 r r - - - - - - r r nand r, i 0 1 0 1 1 0 r r i i i i i i i i IR / IR /
  • 11. 11 Tutorial I • Step 3: Prepare to modify cpualu.vhd – The ALU is realized by a multiplexer selecting one function: r <= a+b when op(4 downto 3)="01" and op(1 downto 0)="00" else sub(7 downto 0) when op(4 downto 3)="01" and op(1 downto 0)="01" else a and b when op(4 downto 3)="01" and op(1 downto 0)="10" else a or b when op(4 downto 3)="01" and op(1 downto 0)="11" else a xor b when op(4 downto 3)="10" and op(1 downto 0)="00" else not a when op(4 downto 0)="11000" else '0'&a(7 downto 1) when op(4 downto 0)="11001" else a(0)&a(7 downto 1) when op(4 downto 0)="11010" else a(7)&a(7 downto 1) when op(4 downto 0)="11011" else a(6 downto 0)&a(7) when op(4 downto 0)="11100" else "00000000"; – We must add an input to this multiplexer to get the NAND of inputs a and b when the opcode is 010 and ALUop is 10 – The current ALU uses op(4..0) to select the operation. – Where does op come from? Read cpu.vhd to find out! – op is actually instruction(14..10)
  • 12. 12 Tutorial I • Step 4: Modify cpualu.vhd – We want to execute the NAND when instruction(15..10) is "010010"..... (is this correct??!) – So op(4..0)="10010" for a NAND – Thus modify the ALU as follows: r <= a+b when op(4 downto 3)="01" and op(1 downto 0)="00" else sub(7 downto 0) when op(4 downto 3)="01" and op(1 downto 0)="01" else a and b when op(4 downto 3)="01" and op(1 downto 0)="10" else a or b when op(4 downto 3)="01" and op(1 downto 0)="11" else a xor b when op(4 downto 3)="10" and op(1 downto 0)="00" else a nand b when op(4 downto 0)="10010" else not a when op(4 downto 0)="11000" else '0'&a(7 downto 1) when op(4 downto 0)="11001" else a(0)&a(7 downto 1) when op(4 downto 0)="11010" else a(7)&a(7 downto 1) when op(4 downto 0)="11011" else a(6 downto 0)&a(7) when op(4 downto 0)="11100" else "00000000";
  • 13. 13 Tutorial I • Step 5: create a test program – We want to test the register mode and the immediate mode! – We use the instruction table to find the encoding -- register mode mov ra,a5h 10a5 mov rb,aah 11aa nand ra,rb 4801 -- expected result: 5F mov rc,83h 1283 nand rc,84h 5a84 -- expected result: 7F Custom ALU instruction opcode ALU op dreg immedite / reg nand r, r 0 1 0 0 1 0 r r - - - - - - r r nand r, i 0 1 0 1 1 0 r r i i i i i i i i IR /
  • 14. 14 Tutorial I • Step 6: test the program – Edit the memory to fill it with the instructions: Address Data 00 10 01 A5 02 11 03 AA 04 48 05 01 06 12 07 83 08 5A 09 84
  • 15. 15 Tutorial I • Step 7: fix bugs – The program works with register source, but not with immediate source! – The error is in this line in the multiplexer in cpualu.vhd: a nand b when op(4 downto 0)="10010" else – Can you fix this?
  • 16. 16 Tutorial II • The previous instruction was combinational. We will now see how to make an ALU instruction whose internal function is clock dependent • Let's say we want an instruction "tick" that returns the number of clock cycles since the processor was turned on. • We will implement this with a circuit counting up at each clock cycle, with a synchronous reset • The counter output will be connected to one input of the ALU multiplexer and the "tick" instruction will select that input
  • 17. 17 Tutorial II • Step 1: define which instruction code corresponds to the new function – As before the opcode has to be 001, 010, 011 (ALU operation), and there is space in the opcodes 010 and 011 – To keep some logic in the instruction set we use opcode 011 this time: all instructions with this opcode have a single operand, as does "tick" • Step 2: Decide which ALUop to use for the selected opcode – ALUop 000 is not, 001 is shr, 010 is ror, 011 is asr, 100 is rol. – Let's choose the next free ALUop: 101 – We can update our instruction table: Instructions instruction(15..8) Instruction(7..0) Custom ALU instructions opcode ALU op dreg tick 0 1 1 1 0 1 r r - - - - - - - -
  • 18. 18 Tutorial II • Step 3: Create the tick counter: a counter with reset – We could create a dedicated VHDL component (in a separate file), but we are lazy here and put everything in cpualu.vhd – Add a signal in cpualu.vhd: tick as an 8-bit std_logic_vector – Add the following counter code somewhere in the architecture: • We use a handy VHDL operation for the addition! • This is a D flip-flop with synchronous reset and an increment process(clk) begin if rising_edge(clk) then if rst='1' then tick<="00000000"; else tick<=tick+1; end if; end if; end process;
  • 19. 19 Tutorial II • Step 4: Modify the multiplexer – Select "tick", the output of the counter, when instruction(14..10) is "11101" r <= a+b when op(4 downto 3)="01" and op(1 downto 0)="00" else sub(7 downto 0) when op(4 downto 3)="01" and op(1 downto 0)="01" else a and b when op(4 downto 3)="01" and op(1 downto 0)="10" else a or b when op(4 downto 3)="01" and op(1 downto 0)="11" else a xor b when op(4 downto 3)="10" and op(1 downto 0)="00" else not a when op(4 downto 0)="11000" else '0'&a(7 downto 1) when op(4 downto 0)="11001" else a(0)&a(7 downto 1) when op(4 downto 0)="11010" else a(7)&a(7 downto 1) when op(4 downto 0)="11011" else a(6 downto 0)&a(7) when op(4 downto 0)="11100" else tick when op(4 downto 0)="11101" else "00000000";
  • 20. 20 Tutorial II • Step 5: Create a test program. – We will create a loop with register RB that goes from 0 to 4 – We will put the number of clock cycles to complete the look in d (we can verify later that this is correct!) – Then the program continuously loads c with the tick 0 mov rb,00 1100 2 add rb,1 3101 4 cmp rb,4 5504 6 jb 2 BB02 8 tick rd 7700 A tick rc 7600 C jmp A B00A
  • 21. 21 Tutorial II • Step 6: Fill in the memory, reset the CPU and test the program – What do you obtain in register C eventually? – Is this what you expect from the program? Verify this by counting the number of instructions executed in the program! Hint: the program loops 4 times the instructions between address 2 and 6.... – You may find that your count is off by one cycle... can you explain this?
  • 22. 22 Problem 1: Random Number Generator (coursework assignment) • Creating a pseudo-random number can be realized with a linear feedback shift register: • The 8-bit number represented by bits r7..r0 is pseudo-random • In reality the sequence is periodical • If the taps of the XOR are well chosen (as in this circuit) the periodicity can be of maximal length (here 255 clocks) • Note that this circuit must be initialized properly: upon reset, at least one bit must be one. Let's say r7=1 on reset
  • 23. 23 Problem 1: Random Number Generator (coursework assignment) • The objective is to add an instruction to the ALU that returns a random number using the LFSR circuit and demonstrate that it works with a test program • You are free to select any unused instruction encoding • Check the coursework assignment for details on what you must include in the report. • Hint: you can create a standalone VHDL module to test the LFSR independently of the processor, and then instantiate the circuit in the ALU