fpga4student.com fpga4student.com - Verilog implementation of a Microcontroller (Part 1 - Specification of the MCU)

Friday, November 25, 2016

Verilog implementation of a Microcontroller (Part 1 - Specification of the MCU)


In this project, Verilog code for a microcontroller is presented. Before implementing, the instruction set architecture for the microcontroller is presented in this part.




Verilog code for microcontroller

Instruction Set Architecture:


Each instruction is 12 bits. There are 3 types of instructions by encoding, shown as following:

  • M type: one operand is accumulator (sometimes ignored) and the other operand is from data memory; the result can be stored into accumulator or the data memory entry (same entry as the second operand).
  • I type: one operand is accumulator and the other operand is immediate number encoded in instruction; the result is stored into accumulator.
  • S type: special instruction, no operand required. (e.g. NOP)



The instruction encoding space is shown in the following table.



Code space
(binary)
Code space
(hex)
Type
Number of Instructions
Note
0000_0000_0000 - 0000_1111_1111
000-0FF
special instructions  (S type)
256
Currently only NOP
used,255 free slots
0001_0000_0000 - 0001_1111_1111
100-1FF
unconditional jump
(I type)
1
GOTO
0010_0000_0000 - 0011_1111_1111
200-3FF
ALU instructions
(M type)
32
16 instructions, 2 destination choices
0100_0000_0000 - 0111_1111_1111
400-7FF
conditional jump
(I type)
4
JZ, JC, JS, JC
1000_0000_0000 - 1111_1111_1111
800-FFF
ALU instructions
(I type)
8
Currently 7 used, 1 free
slot



These instructions can be grouped into 4 categories by function.

1. ALU instruction: using ALU to compute result;

2. Unconditional branch: the GOTO instruction;

3. Conditional branch: the JZ, JC, JS, JO instruction;

4. Special instruction: the NOP.



M type instructions



The general format of M type instruction is shown as following.



Verilog code for microcontroller



The following table contains the detailed information of each M type instruction. Note that “aaaa” encodes the 4 bit address of data memory, and the “d” bit means destination of the result, i.e. if d = 1, result is written to Acc, otherwise the result is written to the same memory location as the operand

Note that all M type instructions are ALU instructions.


Verilog code for microcontroller

Verilog code for microcontroller



microcontroller verilog

Verilog code for microcontroller

Comments:
1. For circulative shift, only the operand from memory entry is rotated, i.e. flags are not involved. For example, if 8’b1000_0000 is shifted left circulative for 1 bit, it becomes 8’b0000_0001, and no flag is affected.
2. For logical shift instructions, 0 is always filled in, and C flag is set to the last bit that is shifted out. For example, if 8’b1000_0000 is shifted left logically for 1 bit, it becomes 8’b0000_0000, and C = 1. Another example, if 8’b0000_0100 is shifted right logically for 3 bit, it becomes 8’b0000_0000, and C = 1. If no bit is shifted out, Z flag and C flag are not affected.
3. For arithmetic shift right instructions, the MSB is filled in, and C flag is set to the last bit that is shifted out. For example, if 8’b1000_0001 is shifted right arithmetically for 1 bit, it becomes 8’b1100_0000, and C = 1. Another example, if 8’b0000_0100 is shifted right arithmetically for 3 bit, it becomes 8’b0000_0000, and C = 1. If no bit is shifted out, Z flag and C flag are not affected.

I type instructions

The general format of M type instruction is shown as following.

Verilog code for microcontroller

The following table contains the detailed information of each M type instruction.
Note that I type instructions contains unconditional branch, conditional branch, and ALU instructions.

Verilog code for microcontroller
Verilog code for microcontroller

S type instructions

The general format of S type instruction is shown as following.
Verilog code for microcontroller
There is only one S type instruction, i.e. the NOP instruction.
Verilog code for microcontroller

Sample Testing Program

After completion the hardware implementation of the MCU, you need to write testbench and load instructions to verify the design by simulation. Below is some testing programs to verify the MCU.

Test1: Program loading, accumulator, and memory loading, GOTO

Verilog code for microcontroller

Test2: Addition and subtraction (M type)

Verilog code for microcontroller

Test3: Logic operation (M type)

Verilog code for microcontroller

Test4: Addition, subtraction, logic operation (I type)

Verilog code for microcontroller

Part 3 will be the Verilog code for the whole microcontroller.

You might also like this:
VHDL code for D Flip Flop
Verilog code for D Flip Flop
Verilog code for a comparator
Verilog code for FIFO memory
VHDL code for FIFO memory

2 comments:

  1. Please keep updating the blog via http://www.fpga4student.com/ to get various FPGA projects using Verilog/ VHDL

    ReplyDelete

FPGA/Verilog/VHDL Courses for Students