Skip to content

Commit 911be0d

Browse files
XingfengYangXingfengYang
XingfengYang
authored and
XingfengYang
committed
f
0 parents  commit 911be0d

File tree

8 files changed

+449
-0
lines changed

8 files changed

+449
-0
lines changed

.gitignore

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
cmake-build-debug/
2+
cmake_install.cmake
3+
CmakeFiles
4+
CMakeCache.txt
5+
lc3
6+
lc3.cbp
7+
Makefile
8+
.idea
9+
*/CMakeFiles
10+
*/cmake_install.cmake
11+
*/mian.cpp.o
12+
*/vm.cpp.o

CMakeLists.txt

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
cmake_minimum_required(VERSION 3.0)
2+
3+
set(CMAKE_BUILD_TYPE Debug)
4+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
5+
6+
project(lc3)
7+
8+
add_subdirectory (src)
9+
add_subdirectory (test)

include/vm.h

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//
2+
// Created by XingfengYang on 2019-08-03.
3+
//
4+
5+
#ifndef LC3_VM_H
6+
#define LC3_VM_H
7+
8+
9+
#include <vector>
10+
11+
namespace lc3 {
12+
13+
enum REG {
14+
R0,
15+
R1,
16+
R2,
17+
R3,
18+
R4,
19+
R5,
20+
R6,
21+
R7,
22+
PC, /* program counter */
23+
COND, /* condition flag */
24+
REG_COUNT
25+
};
26+
27+
enum OP_CODE {
28+
OP_BR = 0b0000, /* branch */
29+
OP_ADD = 0b0001, /* add */
30+
OP_LD = 0b0010, /* load */
31+
OP_ST = 0b0011, /* store */
32+
OP_JSR = 0b0100, /* jump register */
33+
OP_AND = 0b0101, /* and */
34+
OP_LDR = 0b0110, /* load register */
35+
OP_STR = 0b0111, /* store register */
36+
OP_RTI = 0b1000, /* not used */
37+
OP_NOT = 0b1001, /* not */
38+
OP_LDI = 0b1010, /* load indirect */
39+
OP_STI = 0b1011, /* store indirect */
40+
OP_JMP = 0b1100, /* jump */
41+
OP_RES = 0b1101, /* reserved (unused) */
42+
OP_LEA = 0b1110, /* load effective address */
43+
OP_TRAP = 0b1111, /* execute trap */
44+
};
45+
46+
enum COND_FLAG {
47+
FL_POS = 1 << 0, /* positive */
48+
FL_ZRO = 1 << 1, /* zero */
49+
FL_NEG = 1 << 2, /* negative */
50+
};
51+
52+
enum Trap{
53+
TRAP_GETC = 0x20, /* get char from keyboard but not echo onto terminal */
54+
TRAP_OUT = 0x21, /* echo a char onto terminal */
55+
TRAP_PUTS = 0x22, /* echo a word string onto terminal */
56+
TRAP_IN = 0x23, /* get char from keyboard and echo onto terminal */
57+
TRAP_PUTSP = 0x24, /* output a byte string */
58+
TRAP_HALT = 0x25, /* halt program */
59+
};
60+
61+
class VM {
62+
private:
63+
bool running = false;
64+
65+
uint16_t *memory; /* memory */
66+
67+
uint16_t registers[REG_COUNT];
68+
69+
private:
70+
71+
/* fetch instruction from memory */
72+
uint16_t fetch();
73+
74+
/* signed extend */
75+
uint16_t signExtend(uint16_t source,uint16_t numberOfDigits);
76+
77+
void updateFlag(uint16_t reg);
78+
79+
/* decode instruction into opcode and args */
80+
uint16_t decode(uint16_t instruction);
81+
82+
/* execute opcode */
83+
void execute(uint16_t opcode, uint16_t instruction);
84+
85+
void executeTrap(uint16_t trapCode);
86+
87+
/* load from memory */
88+
uint16_t readMemory(uint16_t address);
89+
90+
/* write into memory */
91+
uint16_t writeMemory(uint16_t address, uint16_t value);
92+
93+
public:
94+
VM();
95+
96+
void loadProgram(std::vector<uint16_t> prog);
97+
98+
void run();
99+
100+
~VM();
101+
102+
};
103+
104+
};
105+
106+
107+
#endif //LC3_VM_H

src/CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
add_executable(
2+
lc3
3+
mian.cpp
4+
vm.cpp
5+
)

src/mian.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// Created by XingfengYang on 2019-08-03.
3+
//
4+
5+
#include <iostream>
6+
#include "../include/vm.h"
7+
8+
9+
int main(int argc, char *argv[]){
10+
11+
12+
/**
13+
* ADD R0,R0,0
14+
* ADD R1,R1,3
15+
* ADD R0,R1,R1
16+
*/
17+
std::vector<uint16_t> prog;
18+
19+
prog.push_back(0b0001000000100000); /* ADD R0 R0 0 */
20+
prog.push_back(0b0001001001101111); /* ADD R1 R1 15 */
21+
prog.push_back(0b0001000001000001); /* ADD R0 R1 R0 */
22+
prog.push_back(0b1111000000100001); /* trap out R0 */
23+
prog.push_back(0b0001000000100001); /* ADD R0 R0 1 */
24+
prog.push_back(0b1111000000100001); /* trap out R0 */
25+
prog.push_back(0b1111000000100101); /* trap halt */
26+
27+
std::cout<<"=========lc3========"<<std::endl;
28+
lc3::VM vm;
29+
vm.loadProgram(prog);
30+
vm.run();
31+
32+
33+
return 0;
34+
}

0 commit comments

Comments
 (0)