1
0
mirror of https://github.com/rzzzwilson/pymlac.git synced 2025-06-10 09:32:41 +00:00

Got trace working

This commit is contained in:
Ross Wilson
2016-01-21 15:01:55 +07:00
parent c64e8a148b
commit 3eb41c0536
3 changed files with 194 additions and 163 deletions

View File

@@ -10,6 +10,7 @@ import sys
from Globals import *
import Trace
trace = Trace.Trace(TRACE_FILENAME)
class MainCPU(object):
@@ -181,7 +182,6 @@ class MainCPU(object):
return self.main_decode.get(opcode, self.illegal)(indirect,
address,
instruction)
def illegal(self, indirect, address, instruction):
"""Handle an illegal instruction."""
@@ -205,22 +205,22 @@ class MainCPU(object):
def i_LAW_LWC(self, indirect, address, instruction):
if indirect:
self.AC = ~address & WORDMASK
Trace.itrace('LWC', False, address)
trace.itrace('LWC', False, address)
else:
self.AC = address
Trace.itrace('LAW', False, address)
trace.itrace('LAW', False, address)
return 1
def i_JMP(self, indirect, address, instruction):
address = self.memory.eff_address(address, indirect)
self.PC = address & PCMASK
Trace.itrace('JMP', indirect, address)
trace.itrace('JMP', indirect, address)
return 3 if indirect else 2
def i_DAC(self, indirect, address, instruction):
address = self.memory.eff_address(address, indirect)
self.memory.put(self.AC, address, False)
Trace.itrace('DAC', indirect, address)
trace.itrace('DAC', indirect, address)
return 3 if indirect else 2
def i_XAM(self, indirect, address, instruction):
@@ -228,7 +228,7 @@ class MainCPU(object):
tmp = self.memory.fetch(address, False)
self.memory.put(self.AC, address, False)
self.AC = tmp
Trace.itrace('XAM', indirect, address)
trace.itrace('XAM', indirect, address)
return 3 if indirect else 2
def i_ISZ(self, indirect, address, instruction):
@@ -237,34 +237,34 @@ class MainCPU(object):
self.memory.put(value, address, False)
if value == 0:
self.PC = (self.PC + 1) & WORDMASK
Trace.itrace('ISZ', indirect, address)
trace.itrace('ISZ', indirect, address)
return 3 if indirect else 2
def i_JMS(self, indirect, address, instruction):
address = self.memory.eff_address(address, indirect)
self.memory.put(self.PC, address, False)
self.PC = (address + 1) & PCMASK
Trace.itrace('JMS', indirect, address)
trace.itrace('JMS', indirect, address)
return 3 if indirect else 2
def i_AND(self, indirect, address, instruction):
self.AC &= self.memory.fetch(address, indirect)
Trace.itrace('AND', indirect, address)
trace.itrace('AND', indirect, address)
return 3 if indirect else 2
def i_IOR(self, indirect, address, instruction):
self.AC |= self.memory.fetch(address, indirect)
Trace.itrace('IOR', indirect, address)
trace.itrace('IOR', indirect, address)
return 3 if indirect else 2
def i_XOR(self, indirect, address, instruction):
self.AC ^= self.memory.fetch(address, indirect)
Trace.itrace('XOR', indirect, address)
trace.itrace('XOR', indirect, address)
return 3 if indirect else 2
def i_LAC(self, indirect, address, instruction):
self.AC = self.memory.fetch(address, indirect)
Trace.itrace('LAC', indirect, address)
trace.itrace('LAC', indirect, address)
return 3 if indirect else 2
def i_ADD(self, indirect, address, instruction):
@@ -272,7 +272,7 @@ class MainCPU(object):
if self.AC & OVERFLOWMASK:
self.L = (~self.L) & 01
self.AC &= WORDMASK
Trace.itrace('ADD', indirect, address)
trace.itrace('ADD', indirect, address)
return 3 if indirect else 2
def i_SUB(self, indirect, address, instruction):
@@ -282,14 +282,14 @@ class MainCPU(object):
if self.AC & OVERFLOWMASK:
self.L = ~self.L
self.AC &= WORDMASK
Trace.itrace('SUB', indirect, address)
trace.itrace('SUB', indirect, address)
return 3 if indirect else 2
def i_SAM(self, indirect, address, instruction):
samaddr = self.BLOCKADDR(address)
if self.AC == self.memory.fetch(samaddr, indirect):
self.PC = (self.PC + 1) & PCMASK
Trace.itrace('SAM', indirect, address)
trace.itrace('SAM', indirect, address)
return 3 if indirect else 2
def microcode(self, instruction):
@@ -331,140 +331,140 @@ class MainCPU(object):
if instruction & k:
combine.append(op)
Trace.itrace('+'.join(combine), False)
trace.itrace('+'.join(combine), False)
return 1
def i_DLA(self, indirect, address, instruction):
self.displaycpu.DPC = self.AC
Trace.itrace('DLA')
trace.itrace('DLA')
return 1
def i_CTB(self, indirect, address, instruction):
Trace.itrace('CTB')
trace.itrace('CTB')
return 1
def i_DOF(self, indirect, address, instruction):
self.displaycpu.stop()
Trace.itrace('DOF')
trace.itrace('DOF')
return 1
def i_KRB(self, indirect, address, instruction):
self.AC |= self.kbd.read()
Trace.itrace('KRB')
trace.itrace('KRB')
return 1
def i_KCF(self, indirect, address, instruction):
self.kbd.clear()
Trace.itrace('KCF')
trace.itrace('KCF')
return 1
def i_KRC(self, indirect, address, instruction):
self.AC |= self.kbd.read()
self.kbd.clear()
Trace.itrace('KRC')
trace.itrace('KRC')
return 1
def i_RRB(self, indirect, address, instruction):
self.AC |= self.ttyin.read()
Trace.itrace('RRB')
trace.itrace('RRB')
return 1
def i_RCF(self, indirect, address, instruction):
self.ttyin.clear()
Trace.itrace('RCF')
trace.itrace('RCF')
return 1
def i_RRC(self, indirect, address, instruction):
self.AC |= self.ttyin.read()
self.ttyin.clear()
Trace.itrace('RRC')
trace.itrace('RRC')
return 1
def i_TPR(self, indirect, address, instruction):
self.ttyout.write(self.AC & 0xff)
Trace.itrace('TPR')
trace.itrace('TPR')
return 1
def i_TCF(self, indirect, address, instruction):
self.ttyout.clear()
Trace.itrace('TCF')
trace.itrace('TCF')
return 1
def i_TPC(self, indirect, address, instruction):
self.ttyout.write(self.AC & 0xff)
self.ttyout.clear()
Trace.itrace('TPC')
trace.itrace('TPC')
return 1
def i_HRB(self, indirect, address, instruction):
self.AC |= self.ptrptp.read()
Trace.itrace('HRB')
trace.itrace('HRB')
return 1
def i_HOF(self, indirect, address, instruction):
self.ptrptp.stop()
Trace.itrace('HOF')
trace.itrace('HOF')
return 1
def i_HON(self, indirect, address, instruction):
self.ptrptp.start()
Trace.itrace('HON')
trace.itrace('HON')
return 1
def i_STB(self, indirect, address, instruction):
Trace.itrace('STB')
trace.itrace('STB')
return 1
def i_SCF(self, indirect, address, instruction):
self.Sync40Hz = 0
Trace.itrace('SCF')
trace.itrace('SCF')
return 1
def i_IOS(self, indirect, address, instruction):
Trace.itrace('IOS')
trace.itrace('IOS')
return 1
def i_IOT101(self, indirect, address, instruction):
Trace.itrace('IOT101')
trace.itrace('IOT101')
return 1
def i_IOT111(self, indirect, address, instruction):
Trace.itrace('IOT111')
trace.itrace('IOT111')
return 1
def i_IOT131(self, indirect, address, instruction):
Trace.itrace('IOT131')
trace.itrace('IOT131')
return 1
def i_IOT132(self, indirect, address, instruction):
Trace.itrace('IOT132')
trace.itrace('IOT132')
return 1
def i_IOT134(self, indirect, address, instruction):
Trace.itrace('IOT134')
trace.itrace('IOT134')
return 1
def i_IOT141(self, indirect, address, instruction):
Trace.itrace('IOT141')
trace.itrace('IOT141')
return 1
def i_IOF(self, indirect, address, instruction):
Trace.itrace('IOF')
trace.itrace('IOF')
return 1
def i_ION(self, indirect, address, instruction):
Trace.itrace('ION')
trace.itrace('ION')
return 1
def i_PPC(self, indirect, address, instruction):
self.ptrptp.punch(self.AC & 0xff)
Trace.itrace('PPC')
trace.itrace('PPC')
return 1
def i_PSF(self, indirect, address, instruction):
if self.ptrptp.ready():
self.PC = (self.PC + 1) & WORDMASK
Trace.itrace('PSF')
trace.itrace('PSF')
return 1
def i_RAL1(self, indirect, address, instruction):
@@ -472,7 +472,7 @@ class MainCPU(object):
newac = (self.AC << 1) | self.L
self.L = newl
self.AC = newac & WORDMASK
Trace.itrace('RAL', False, 1)
trace.itrace('RAL', False, 1)
return 1
def i_RAL2(self, indirect, address, instruction):
@@ -484,7 +484,7 @@ class MainCPU(object):
newac = (self.AC << 1) | self.L
self.L = newl
self.AC = newac & WORDMASK
Trace.itrace('RAL', False, 2)
trace.itrace('RAL', False, 2)
return 1
def i_RAL3(self, indirect, address, instruction):
@@ -500,7 +500,7 @@ class MainCPU(object):
newac = (self.AC << 1) | self.L
self.L = newl
self.AC = newac & WORDMASK
Trace.itrace('RAL', False, 3)
trace.itrace('RAL', False, 3)
return 1
def i_RAR1(self, indirect, address, instruction):
@@ -508,7 +508,7 @@ class MainCPU(object):
newac = (self.AC >> 1) | (self.L << 15)
self.L = newl
self.AC = newac & WORDMASK
Trace.itrace('RAR', False, 1)
trace.itrace('RAR', False, 1)
return 1
def i_RAR2(self, indirect, address, instruction):
@@ -520,7 +520,7 @@ class MainCPU(object):
newac = (self.AC >> 1) | (self.L << 15)
self.L = newl
self.AC = newac & WORDMASK
Trace.itrace('RAR', False, 2)
trace.itrace('RAR', False, 2)
return 1
def i_RAR3(self, indirect, address, instruction):
@@ -536,41 +536,41 @@ class MainCPU(object):
newac = (self.AC >> 1) | (self.L << 15)
self.L = newl
self.AC = newac & WORDMASK
Trace.itrace('RAR', False, 3)
trace.itrace('RAR', False, 3)
return 1
def i_SAL1(self, indirect, address, instruction):
high_bit = self.AC & HIGHBITMASK
value = self.AC & 037777
self.AC = (value << 1) | high_bit
Trace.itrace('SAL', False, 1)
trace.itrace('SAL', False, 1)
return 1
def i_SAL2(self, indirect, address, instruction):
high_bit = self.AC & HIGHBITMASK
value = self.AC & 017777
self.AC = (value << 2) | high_bit
Trace.itrace('SAL', False, 2)
trace.itrace('SAL', False, 2)
return 1
def i_SAL3(self, indirect, address, instruction):
high_bit = self.AC & HIGHBITMASK
value = self.AC & 007777
self.AC = (value << 3) | high_bit
Trace.itrace('SAL', False, 3)
trace.itrace('SAL', False, 3)
return 1
def i_SAR1(self, indirect, address, instruction):
high_bit = self.AC & HIGHBITMASK
self.AC = (self.AC >> 1) | high_bit
Trace.itrace('SAR', False, 1)
trace.itrace('SAR', False, 1)
return 1
def i_SAR2(self, indirect, address, instruction):
high_bit = self.AC & HIGHBITMASK
self.AC = (self.AC >> 1) | high_bit
self.AC = (self.AC >> 1) | high_bit
Trace.itrace('SAR', False, 2)
trace.itrace('SAR', False, 2)
return 1
def i_SAR3(self, indirect, address, instruction):
@@ -578,120 +578,120 @@ class MainCPU(object):
self.AC = (self.AC >> 1) | high_bit
self.AC = (self.AC >> 1) | high_bit
self.AC = (self.AC >> 1) | high_bit
Trace.itrace('SAR', False, 3)
trace.itrace('SAR', False, 3)
return 1
def i_DON(self, indirect, address, instruction):
self.displaycpu.DRSindex = 0
self.displaycpu.start()
Trace.itrace('DON')
trace.itrace('DON')
return 1
def i_ASZ(self):
if self.AC == 0:
self.PC = (self.PC + 1) & WORDMASK
Trace.itrace('ASZ')
trace.itrace('ASZ')
return 1
def i_ASN(self):
if self.AC != 0:
self.PC = (self.PC + 1) & WORDMASK
Trace.itrace('ASN')
trace.itrace('ASN')
return 1
def i_ASP(self):
if not (self.AC & HIGHBITMASK):
self.PC = (self.PC + 1) & WORDMASK
Trace.itrace('ASP')
trace.itrace('ASP')
return 1
def i_ASM(self):
if (self.AC & HIGHBITMASK):
self.PC = (self.PC + 1) & WORDMASK
Trace.itrace('ASM')
trace.itrace('ASM')
return 1
def i_LSZ(self):
if self.L == 0:
self.PC = (self.PC + 1) & WORDMASK
Trace.itrace('LSZ')
trace.itrace('LSZ')
return 1
def i_LSN(self):
if self.L != 0:
self.PC = (self.PC + 1) & WORDMASK
Trace.itrace('LSN')
trace.itrace('LSN')
return 1
def i_DSF(self):
if self.displaycpu.ison():
self.PC = (self.PC + 1) & WORDMASK
Trace.itrace('DSF')
trace.itrace('DSF')
return 1
def i_DSN(self):
if not self.displaycpu.ison():
self.PC = (self.PC + 1) & WORDMASK
Trace.itrace('DSN')
trace.itrace('DSN')
return 1
def i_KSF(self):
if self.kbd.ready():
self.PC = (self.PC + 1) & WORDMASK
Trace.itrace('KSF')
trace.itrace('KSF')
return 1
def i_KSN(self):
if not self.kbd.ready():
self.PC = (self.PC + 1) & WORDMASK
Trace.itrace('KSN')
trace.itrace('KSN')
return 1
def i_RSF(self):
if self.ttyin.ready():
self.PC = (self.PC + 1) & WORDMASK
Trace.itrace('RSF')
trace.itrace('RSF')
return 1
def i_RSN(self):
if not self.ttyin.ready():
self.PC = (self.PC + 1) & WORDMASK
Trace.itrace('RSN')
trace.itrace('RSN')
return 1
def i_TSF(self):
if self.ttyout.ready():
self.PC = (self.PC + 1) & WORDMASK
Trace.itrace('TSF')
trace.itrace('TSF')
return 1
def i_TSN(self):
if not self.ttyout.ready():
self.PC = (self.PC + 1) & WORDMASK
Trace.itrace('TSN')
trace.itrace('TSN')
return 1
def i_SSF(self):
if self.display.ready(): # skip if 40Hz sync on
self.PC = (self.PC + 1) & WORDMASK
Trace.itrace('SSF')
trace.itrace('SSF')
return 1
def i_SSN(self):
if not self.display.ready():
self.PC = (self.PC + 1) & WORDMASK
Trace.itrace('SSN')
trace.itrace('SSN')
return 1
def i_HSF(self):
if self.ptrptp.ready():
self.PC = (self.PC + 1) & WORDMASK
Trace.itrace('HSF')
trace.itrace('HSF')
return 1
def i_HSN(self):
if not self.ptrptp.ready():
self.PC = (self.PC + 1) & WORDMASK
Trace.itrace('HSN')
trace.itrace('HSN')
return 1

View File

@@ -2,89 +2,129 @@
"""
The Imlac trace stuff.
Simple usage:
import Trace
trace = Trace.Trace('my_log.log', maincpu, dispcpu)
trace.itrace(msg)
Based on the 'borg' recipe from [http://code.activestate.com/recipes/66531/].
"""
import time
import datetime
import os
from Globals import *
# module-level state variables
tracing = False
tracefile = None
cpu = None
dcpu = None
class Trace(object):
def init(filename, maincpu, displaycpu):
global tracing, tracefile, cpu, dcpu
__shared_state = {} # this __dict__ shared by ALL instances
tracing = True
tracefile = open(filename, 'wa')
trace('%s trace\n%s\n' % (PYMLAC_VERSION, '-'*60))
tracing = False
comment = None
def __init__(self, filename, maincpu=None, displaycpu=None):
"""Initialize the trace:
cpu = maincpu
dcpu = displaycpu
filename name of the trace file
maincpu the main CPU object (may be added later)
displaycpu the display CPU object (may be added later)
"""
def close():
global tracing, tracefile
# ensure same state as all other instances
self.__dict__ = Trace.__shared_state
tracefile.close()
tracing = False
tracefile = None
# set internal state
self.tracing = True
self.tracefile = filename
try:
os.remove(filename)
except:
pass
self.tracefile = open(filename, 'wb')
self.tracefile.write('%s trace\n%s\n' % (PYMLAC_VERSION, '-'*60))
def trace(msg):
if tracing:
tracefile.write(msg)
def deimtrace(opcode, code):
if tracing:
tracefile.write('%s\t%s\t' % (opcode, code))
tracefile.flush()
self.cpu = maincpu
self.dcpu = displaycpu
def dtrace(opcode, address=None):
if tracing:
if address is None:
tracefile.write('%s\t\t' % opcode)
else:
tracefile.write('%s\t%5.5o\t' % (opcode, address))
tracefile.flush()
def add_maincpu(self, maincpu):
"""Add the main CPU object."""
def itrace(opcode, indirect=False, address=None):
if tracing:
char = '*' if indirect else ''
if address is None:
tracefile.write('%s\t%s\t' % (opcode, char))
else:
tracefile.write('%s\t%s%5.5o\t' % (opcode, char, address))
tracefile.flush()
self.cpu = maincpu
def itraceend(dispon):
if dispon:
trace('L=%1.1o AC=%6.6o DX=%5.5o DY=%6.6o\n' %
(cpu.L, cpu.AC, dcpu.DX, dcpu.DY))
else:
trace('L=%1.1o AC=%6.6o\n' % (cpu.L, cpu.AC))
def add_dispcpu(self, dispcpu):
"""Add the display CPU object."""
# time the instruction execution
# # get time
# to = datetime.datetime.now()
# hr = to.hour
# min = to.minute
# sec = to.second
# msec = to.microsecond
# trace(' %02d:%02d:%02d.%06d' % (hr, min, sec, msec))
self.dcpu = dispcpu
tracefile.flush()
def close(self):
"""Close trace."""
def comment(msg):
tracefile.write(msg+'\n')
tracefile.flush()
self.tracefile.close()
self.tracing = False
self.tracefile = None
def settrace(new_tracing):
global tracing
def deimtrace(self, opcode, code):
"""Trace the DEIM instruction.
tracing = new_tracing
opcode DEIM opcode
code the operation
"""
if self.tracing:
self.tracefile.write('%s\t%s\t' % (opcode, code))
self.tracefile.flush()
def dtrace(self, opcode, address=None):
"""Trace the display CPU.
opcode display opcode
address address for the opcode
"""
if self.tracing:
if address is None:
self.tracefile.write('%s\t\t' % opcode)
else:
self.tracefile.write('%s\t%5.5o\t' % (opcode, address))
self.tracefile.flush()
def itrace(self, opcode, indirect=False, address=None):
"""Main CPU trace.
opcode the main CPU opcode
indirect True if instruction was indirect
adress address for the instruction (if any)
"""
if self.tracing:
char = '*' if indirect else ''
if address is None:
self.tracefile.write('%s\t%s\t' % (opcode, char))
else:
self.tracefile.write('%s\t%s%5.5o\t' % (opcode, char, address))
self.tracefile.flush()
def itraceend(self, dispon):
"""Trace at the end of one execution cycle.
dispon True if the display was on
"""
self.tracefile.write('L=%1.1o AC=%6.6o PC=%6.6o'
% (self.cpu.L, self.cpu.AC, self.cpu.PC))
if dispon:
self.tracefile.write(' DX=%5.5o DY=%6.6o'
% (self.dcpu.DX, self.dcpu.DY))
self.tracefile.write('\n')
self.tracefile.flush()
def comment(self, msg):
"""Add a comment to the trace."""
self.tracefile.write(msg+'\n')
self.tracefile.flush()
def settrace(self, new_tracing):
"""Set the trace ON or OFF."""
self.tracing = new_tracing

View File

@@ -26,8 +26,7 @@ import Memory
import PtrPtp
import Trace
import log
log = log.Log('test_CPU.log', log.Log.DEBUG)
trace = Trace.Trace(TRACE_FILENAME)
class TestCPU(object):
@@ -247,6 +246,7 @@ class TestCPU(object):
self.used_cycles= 0
for _ in range(number):
cycles = self.cpu.execute_one_instruction()
trace.itraceend(False)
self.ptrptp.ptr_tick(cycles)
self.ptrptp.ptp_tick(cycles)
self.used_cycles += cycles
@@ -269,6 +269,7 @@ class TestCPU(object):
self.used_cycles= 0
while True:
cycles = self.cpu.execute_one_instruction()
trace.itraceend(False)
self.ptrptp.ptr_tick(cycles)
self.ptrptp.ptp_tick(cycles)
self.used_cycles += cycles
@@ -472,17 +473,6 @@ class TestCPU(object):
else:
raise Exception('setd: bad state: %s' % str(state))
def debug_operation(self, op, var1, var2):
"""Write operation to log file."""
if var1:
if var2:
log.debug('Operation: %s %s %s' % (op, var1, var2))
else:
log.debug('Operation: %s %s' % (op, var1))
else:
log.debug('Operation: %s' % op)
def execute(self, test, filename):
"""Execute test string in 'test'."""
@@ -501,13 +491,18 @@ class TestCPU(object):
self.cpu.running = True
self.display_state = False
trace_filename = filename + '.trace'
Trace.init(trace_filename, self.cpu, None)
# prepare the trace
trace.add_maincpu(self.cpu)
# clear registers and memory to 0 first
self.allreg(self.reg_all_value, None)
self.allmem(self.mem_all_value, None)
# show the DSL we are about execute
trace.comment('')
trace.comment(test)
trace.comment('-'*80)
# interpret the test instructions
suite = test.split(';')
for instruction in suite:
@@ -525,8 +520,6 @@ class TestCPU(object):
fld1 = fields[1].strip().lower()
fld2 = fields[2].strip().lower()
self.debug_operation(opcode, fld1, fld2)
# cll the correct DSL primitive
if opcode == 'setreg':
r = self.setreg(fld1, fld2)
@@ -600,8 +593,6 @@ class TestCPU(object):
def main(self, filename):
"""Execute CPU tests from 'filename'."""
log.debug("Running test file '%s'" % filename)
# get all tests from file
with open(filename, 'rb') as fd:
lines = fd.readlines()
@@ -622,8 +613,9 @@ class TestCPU(object):
if line[0] in ('\t', ' '): # continuation
if test:
test += '; '
test += line[1:]
if not test.endswith(';'):
test += ';'
test += ' ' + line[1:].strip()
else: # beginning of new test
if test:
tests.append(test)
@@ -636,7 +628,6 @@ class TestCPU(object):
# now do each test
for test in tests:
self.show_progress()
log.debug('Executing test: %s' % test)
self.execute(test, filename)
################################################################################