FPGA projects, Verilog projects, VHDL projects

Verilog code for Microcontroller (Part 1 - Specification)

In this project, Verilog code for a microcontroller is presented. Before implementing, the instruction set architecture for the microcontroller is presented in this part. (Part-2 and Part-3)


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.
2. Verilog code for FIFO memory
3. Verilog code for 16-bit single-cycle MIPS processor
4. Programmable Digital Delay Timer in Verilog HDL
5. Verilog code for basic logic components in digital circuits
6. Verilog code for 32-bit Unsigned Divider
7. Verilog code for Fixed-Point Matrix Multiplication
8. Plate License Recognition in Verilog HDL
9. Verilog code for Carry-Look-Ahead Multiplier
10. Verilog code for a Microcontroller
11. Verilog code for 4x4 Multiplier
12. Verilog code for Car Parking System
13. Image processing on FPGA using Verilog HDL
14. How to load a text file into FPGA using Verilog HDL
15. Verilog code for Traffic Light Controller
16. Verilog code for Alarm Clock on FPGA
17. Verilog code for comparator design
18. Verilog code for D Flip Flop
19. Verilog code for Full Adder
20. Verilog code for counter with testbench
21. Verilog code for 16-bit RISC Processor
22. Verilog code for button debouncing on FPGA
23. How to write Verilog Testbench for bidirectional/ inout ports
28. Verilog code for Decoder
29. Verilog code for Multiplexers
Verilog VHDL Tool

No comments:

Post a Comment