Dsu Micro Project
Dsu Micro Project
1
MAHARASHTRA STATE BOARD OF TECHNICAL
EDUCATION
Certificate
2
Instruction set Implementation
Introduction
3
•
Objectives
Instructions:
1. Define the instruction set: Choose a set of simple instructions such as
MOV (move), ADD (addition), SUB (subtraction), JMP (unconditional
jump), JZ (jump if zero), and HLT (halt).
2. Design the instruction encoding: Decide on the format for encoding each
instruction. You could use fixed-length encoding or variable-length
encoding depending on the complexity of your instructions.
3. Implement the emulator: Write code to simulate the execution of
instructions. Create data structures to represent the CPU state including
registers, memory, and the program counter. Implement functions to fetch,
decode, and execute instructions based on the opcode.
4
4. Write sample programs: Create a few sample programs using the defined
instruction set. These programs should demonstrate the functionality of
various instructions and control flow constructs.
5. Test the emulator: Run the sample programs through the emulator to
verify that the instructions are executed correctly and produce the expected
results.
6. Document the project: Write documentation detailing the instruction set,
encoding format, emulator implementation, and sample programs. Include
any assumptions made during the design process and explain how to use the
emulator.
5
Sample Code (in C):
```c
#include <stdio.h>
Typedef struct {
Int pc; // Program counter
Int regs[4]; // Registers
Int mem[MEM_SIZE]; // Memory
} CPU;
6
Void execute_instruction(CPU *cpu) {
// Fetch instruction from memory
Int instr = cpu->mem[cpu->pc];
// Decode opcode
Int opcode = instr >> 16;
// Execute instruction
Switch (opcode) {
Case 0: // MOV
{
Int dest_reg = (instr >> 12) & 0xF;
Int src_reg = (instr >> 8) & 0xF;
Cpu->regs[dest_reg] = cpu->regs[src_reg];
}
Break;
// Add support for other instructions (ADD, SUB, JMP, JZ, HLT)
similarly
Default:
Printf(“Invalid opcode\n”);
7
Break;
}
// Increment program counter
Cpu->pc++;
}
Int main() {
CPU cpu = {0}; // Initialize CPU
// Sample program: MOV R0, 42; MOV R1, 10; ADD R0, R1; HLT
Cpu.mem[0] = 0x00004200; // MOV R0, 42
Cpu.mem[1] = 0x00011001; // MOV R1, 10
Cpu.mem[2] = 0x10000001; // ADD R0, R1
Cpu.mem[3] = 0x20000000; // HLT
// Emulate program execution
While (cpu.mem[cpu.pc] != 0x20000000) { // HLT instruction
Execute_instruction(&cpu);
}
// Output result
8
Printf(“Result: %d\n”, cpu.regs[0]);
Return 0;
}
```
System.out.println(“Correct!”);
Score++;
} else {
System.out.println(“Incorrect!”);
}
System.out.println();
}
Scanner.close();
9
}
}
```
Detail description
10
2. **CPU Structure**:
- A structure named `CPU` is defined to represent the state of the CPU. It
contains:
- `pc`: Program counter, indicating the address of the next instruction to
be executed.
- `regs`: An array representing CPU registers.
- `mem`: An array representing memory.
3. **execute_instruction Function**:
- The `execute_instruction` function simulates the execution of an
instruction by the CPU.
- It takes a pointer to a `CPU` struct as its argument.
- Inside the function:
- The instruction is fetched from memory using the program counter
(`cpu->mem[cpu->pc]`).
- The opcode of the instruction is decoded (`int opcode = instr >> 16`).
- A switch statement is used to handle different opcodes. For each
opcode:
11
- The appropriate action is taken, such as moving data between
registers, performing arithmetic operations, or modifying the program
counter.
- After executing the instruction, the program counter (`cpu->pc`) is
incremented.
4. **Main Function**:
- The `main` function initializes a CPU struct (`CPU cpu`) and simulates
the execution of a sample program.
- The sample program is represented by hardcoded instructions stored in
the `cpu.mem` array.
- The program consists of MOV, ADD, and HLT instructions.
- The `execute_instruction` function is called repeatedly in a loop until the
HLT instruction is encountered.
- Once the program halts, the result of the computation is printed.
5. **Sample Program**:
- The provided sample program demonstrates the usage of MOV, ADD,
and HLT instructions.
- It moves the value 42 into register R0, the value 10 into register R1, adds
the values of R0 and R1, and halts the execution.
12
6. **Execution**:
- To execute the code, it must be compiled using a C compiler (e.g.,
GCC).
- After compilation, the resulting executable can be run to execute the
sample program.
7. **Additional Notes**:
- The code is written in a straightforward manner to demonstrate the basic
concepts of CPU emulation and instruction execution.
- It serves as a foundation for understanding more complex CPU
architectures and emulator implementations.
Overall, this code provides a simple but illustrative example of how a CPU
emulator can be implemented to execute programs written in a custom
instruction set architecture.
13
CONCLUSION
14
Through the stages of defining the instruction set, designing encoding
formats, implementing the emulator, creating sample programs, testing
functionality, and documenting the project, participants develop a
comprehensive understanding of CPU emulation and ISA design principles.
//
15
3 3rd Literature Review
4 4th Collection of Data
5 5th Collection of Data
6 6th Discussion and outline
of Content
7 7th Formulation of Content
8 8th Editing and proof
Reading of content
9 9th Compilation of Report
and Presentation
10 10th Seminar
11 11th Viva Voce
12 12th Final submission of
Micro Project
16
Evaluation Sheet for the Micro Project
....................................................................................................................
Roll No. Student Name Mark out of Mark out of Total out of
6 for 4 for 10
performance preformance
Group Oral /
presentation
activity
18
22 Mansee Masne
19