-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemory.java
More file actions
115 lines (98 loc) · 3.3 KB
/
Memory.java
File metadata and controls
115 lines (98 loc) · 3.3 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
Title: Memory.java
Name: Dylan Kapustka (Dlk190000)
Instructor: Professor Ozbirn
Course: CS 4348.001 - S21
Date: 03/09/2021
Description: This class simulates Memory and can read or write based on CPU instructions.
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Memory
{
final static int [] memory = new int[2000]; //Implements memory
public static void main(String args[])
{
try
{
//Scan our file
Scanner reader = new Scanner(System.in);
File file = null;
if(reader.hasNextLine())
{
file = new File(reader.nextLine());
}
//Try to read the file
//Initialize memory
try
{
Scanner scanner = new Scanner(file);
String tmp;
int instruction;
int init = 0;
//Read file and load instruction
while(scanner.hasNext())
{
if(scanner.hasNextInt())
{
instruction = scanner.nextInt();
memory[init++] = instruction;
}
else
{
tmp = scanner.next();
//if token starts with ".", move counter
if(tmp.charAt(0) == '.')
{
init = Integer.parseInt(tmp.substring(1));
}
//if token is comment or anything else, go to next line
else
{
scanner.nextLine();
}
}
}
scanner.close();
}
catch (FileNotFoundException ex)
{
ex.printStackTrace();
}
String line; //holds scanner lines
//This loop keeps reading instructions and performs read or write functions
while(true)
{
if(reader.hasNext())
{
line = reader.nextLine();
String[] tokens = line.split(":"); //split the line to get the necessary tokens
String instruction = tokens[0]; //tells if READ or WRITE
//If first token is 'READ', CPU is requesting to read
if(instruction.equals("READ"))
{
//Send to CPU
System.out.println(memory[Integer.parseInt(tokens[1])]);
}
//Else, CPU is requesting to write
else if(instruction.equals("WRITE"))
{
//Write to memory
int address = Integer.parseInt(tokens[1]);
int data = Integer.parseInt(tokens[2]);
memory[address] = data;
}
else
break;
}
else
break;
}
reader.close();
} catch(NumberFormatException e)
{
e.printStackTrace();
}
}
}