Clean code
This commit is contained in:
parent
19ab0b8eb7
commit
ef427a9944
4 changed files with 18 additions and 22 deletions
|
@ -1,13 +1,15 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
|
|
||||||
|
from components.ram import Ram
|
||||||
|
|
||||||
class Caretaker:
|
class Caretaker:
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self,ramSize):
|
||||||
self.objects=dict() # Create empty objects pool
|
self.objects=dict() # Create empty objects pool
|
||||||
# Add registers to pool
|
# Add registers to pool
|
||||||
for reg in ["MAR","MDR", "PC", "MBR", "SP","LV","CPP","TOS","OPC","H"]:
|
for reg in ["MAR","MDR", "PC", "MBR", "SP","LV","CPP","TOS","OPC","H"]:
|
||||||
self.objects[reg]=0
|
self.objects[reg]=0
|
||||||
self.objects["RAM"]=None
|
self.objects["RAM"]=Ram(self,ramSize)
|
||||||
|
|
||||||
def __getitem__(self,key):
|
def __getitem__(self,key):
|
||||||
if key=="MBRU": # If we ask for unsigned
|
if key=="MBRU": # If we ask for unsigned
|
||||||
|
|
|
@ -8,13 +8,14 @@ class Microprogram:
|
||||||
if self.c["RAM"]==None: # Check if RAM is initialize
|
if self.c["RAM"]==None: # Check if RAM is initialize
|
||||||
raise RuntimeError("Microprogram initialization fail, RAM is not initialized")
|
raise RuntimeError("Microprogram initialization fail, RAM is not initialized")
|
||||||
|
|
||||||
def run(self):
|
def run(self,constantPoolLocation, stackLocation):
|
||||||
"""
|
"""
|
||||||
Start microprogram
|
Start microprogram
|
||||||
"""
|
"""
|
||||||
self.c["LV"]=(1024)# Place stack to 1024
|
self.c["LV"]=stackLocation# Place stack to 1024
|
||||||
self.c["SP"]=(1024-1) # Init SP to LV-1 (because otherwise first element of the stack will be enty because of BIPUSH impl
|
self.c["SP"]=stackLocation-1 # Init SP to LV-1 (because otherwise first element of the stack will be enty because of BIPUSH impl
|
||||||
|
self.c["CPP"]=constantPoolLocation
|
||||||
|
|
||||||
for i in range(1,30): # Launche first 30 insctructions
|
for i in range(1,30): # Launche first 30 insctructions
|
||||||
self.fetch() # Fetch
|
self.fetch() # Fetch
|
||||||
self.c["PC"]+=1 # INC PC after fetch
|
self.c["PC"]+=1 # INC PC after fetch
|
||||||
|
@ -202,6 +203,7 @@ class Microprogram:
|
||||||
self.c["H"]=self.c["MBRU"]|self.c["H"]
|
self.c["H"]=self.c["MBRU"]|self.c["H"]
|
||||||
self.c["PC"]=self.c["OPC"]+self.c["H"]
|
self.c["PC"]=self.c["OPC"]+self.c["H"]
|
||||||
##################
|
##################
|
||||||
|
|
||||||
def F(self): # This function is here just to follow ijvm implementation of "Structured Computer Organization"
|
def F(self): # This function is here just to follow ijvm implementation of "Structured Computer Organization"
|
||||||
self.fetch();self.c["PC"]+=1 # Needed because memory access take 1 cycle in simulation
|
self.fetch();self.c["PC"]+=1 # Needed because memory access take 1 cycle in simulation
|
||||||
self.c["PC"]=self.c["PC"]+1
|
self.c["PC"]=self.c["PC"]+1
|
||||||
|
|
|
@ -11,13 +11,13 @@ class Ram:
|
||||||
"""
|
"""
|
||||||
Load a Ram file into self.data
|
Load a Ram file into self.data
|
||||||
"""
|
"""
|
||||||
data=dict()
|
self.data=dict()
|
||||||
addr=0
|
|
||||||
f=open(filepath,"r")
|
f=open(filepath,"r")
|
||||||
|
addr=0
|
||||||
for line in f.readlines():
|
for line in f.readlines():
|
||||||
line=line.rstrip() # remove \n
|
line=line.rstrip() # remove \n
|
||||||
if line in ijvm:
|
if line in ijvm:
|
||||||
data[addr]=int(ijvm[line])
|
self.data[addr]=int(ijvm[line])
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
value=int(line,0)
|
value=int(line,0)
|
||||||
|
@ -25,11 +25,10 @@ class Ram:
|
||||||
raise ValueError("Invalide RAM entry: Address {} value {}".format(addr,line))
|
raise ValueError("Invalide RAM entry: Address {} value {}".format(addr,line))
|
||||||
if value>255:
|
if value>255:
|
||||||
raise ValueError("Ram contain values that does not fit in a byte: value {} at address {}".format(value,addr))
|
raise ValueError("Ram contain values that does not fit in a byte: value {} at address {}".format(value,addr))
|
||||||
data[addr]=value
|
self.data[addr]=value
|
||||||
addr+=1
|
addr+=1
|
||||||
f.close()
|
f.close()
|
||||||
self.data=data
|
|
||||||
|
|
||||||
def write(self):
|
def write(self):
|
||||||
"""
|
"""
|
||||||
Write data to memory based Mic-1 architecture
|
Write data to memory based Mic-1 architecture
|
||||||
|
|
|
@ -4,15 +4,8 @@ from components.microprogram import Microprogram
|
||||||
from components.ram import Ram
|
from components.ram import Ram
|
||||||
from components.caretaker import Caretaker
|
from components.caretaker import Caretaker
|
||||||
|
|
||||||
c=Caretaker() # Init components
|
c=Caretaker(5000) # Init components (stackLocation)
|
||||||
RAM=Ram(c,5000) # Init ram
|
c["RAM"].loadRamFile("./ram.txt") # Load Ram from file
|
||||||
RAM.loadRamFile("./ram.txt") # Load Ram from file
|
|
||||||
c["RAM"]=RAM # Add ram to components
|
|
||||||
|
|
||||||
|
|
||||||
mic=Microprogram(c) # Create micro program
|
mic=Microprogram(c) # Create micro program
|
||||||
mic.run() # Run the micro program
|
mic.run(800, 1024) # Run the micro program with run(constantPoolLocation,stackLocation)
|
||||||
|
|
||||||
mic.rd()
|
|
||||||
print(bin(c["MDR"]))
|
|
||||||
print(RAM.dump())
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue