-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemory.java
More file actions
74 lines (70 loc) · 1.73 KB
/
Memory.java
File metadata and controls
74 lines (70 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
*
* CS365 Project 2
*
* Contributors: Alex Teepe, Brandon Yen, Austin Saporito, Austin Day, and Charles Rizzo
*
* Entry point for jar file that creates a virtual machine to interpret byte code generated from project 1.
* Can be ran in Verbose mode with -v as second command line arg.
*/
public class Memory {
protected int memory[];
protected int sp;
protected int ip;
protected int size;
/*
0 argument constructor
*/
public Memory() {
size = 1024;
memory = new int[size];
sp = size - 1;
ip = 0;
}
/*
1 argument constructer to change size
*/
public Memory(int sizeInBytes) {
size = sizeInBytes/4;
memory = new int[size];
sp = size - 1;
ip = 0;
}
/*
checks if pop will go out of bounds and returns the first value
*/
public int pop() {
if (sp + 1 >= size) throw new IndexOutOfBoundsException();
return memory[sp++];
}
/*
pushes the value passed to it on the stack
*/
public void push(int val) {
memory[--sp] = val;
}
/*
returns the instruction its on
*/
public int getInstruction() {
return memory[ip];
}
/*
jumps to the label with the offset
*/
public void jump(int offset) {
this.ip += (offset / 4);
}
public void loadInstructions(int[] instr) {
//load instructions from an array of instructions into memory starting at address 0
System.arraycopy(instr, 0, memory, 0, instr.length);
}
/*
returns the value of the stack with the offset and current stack pointer
*/
public int peek(int offset)
{
if (sp + offset >= size) throw new IndexOutOfBoundsException();
return memory[sp + offset];
}
}