aboutsummaryrefslogtreecommitdiff
path: root/components/ram.py
blob: d503929801224d2a6186456af496452ee0e61431 (plain)
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
from components.ijvm import ijvm

class Ram:
	
	def __init__(self,components,size):
		self.data=dict()
		self.lastAddr=size-1
		self.c=components
	
	def loadRamFile(self,filepath):
		data=dict()
		addr=0
		f=open(filepath,"r")
		for line in f.readlines():
			line=line.rstrip() # remove \n
			if line in ijvm:
				data[addr]=int(ijvm[line])
			else:
				data[addr]=int(line,0)
			addr+=1
		f.close()
		self.data=data
	
	def write(self):
		addr=self.c["MAR"]
		if addr>self.lastAddr:
			raise ValueError("You get out of the ram by trying to set a value at address {}, max address is {}".format(addr,self.lastAddr))
		self.data[addr]=self.c["MDR"]
	
	def read(self):
		addr=self.c["MAR"]
		value=None
		try:
			value=self.data[addr]
		except:
			if addr>self.lastAddr:
				raise ValueError("You get out of the ram by trying to get value at address {}, max address is {}".format(addr,self.lastAddr))
		if(value==None):
			return(0)
		return(value)
		
	def fetch(self):
		addr=self.c["PC"]
		value=None
		try:
			value=self.data[addr]
		except:
			if addr>self.lastAddr:
				raise ValueError("You get out of the ram by trying to get value at address {}, max address is {}".format(addr,self.lastAddr))
		if(value==None):
			return(0)
		return(value)
		
	def dump(self):
		for key,value in self.data.items():
			#print("{}:{}".format(key,bin(value)[2:]))
			print("{}:{}".format(key,value))
		
	def dumpRange(self,start,end):
		for i in range(start,end+1):
			try:
				print("{}:{}".format(i,self.data[i]))
			except:
				print("{}:0".format(i))