# HG changeset patch # User Michael Pavone # Date 1548628657 28800 # Node ID 73ac2e59fa3f0cd9ec804b8d499aded5dbbf22ae # Parent 4fd34fde390cff9c4b0a94498355a52c74cc3433 Implemented sbc instruction in CPU DSL diff -r 4fd34fde390c -r 73ac2e59fa3f cpu_dsl.py --- a/cpu_dsl.py Sun Jan 27 05:55:08 2019 -0800 +++ b/cpu_dsl.py Sun Jan 27 14:37:37 2019 -0800 @@ -385,23 +385,32 @@ else: fmt = '\n\t{dst} = {src} & 0x8000 ? {src} | 0xFFFF0000 : {src};' return fmt.format(src=params[1], dst=params[2]) - -def _adcCImpl(prog, params, rawParams): + +def _getCarryCheck(prog): carryFlag = None for flag in prog.flags.flagCalc: if prog.flags.flagCalc[flag] == 'carry': carryFlag = flag if carryFlag is None: raise Exception('adc requires a defined carry flag') - base = '\n\t{dst} = {a} + {b} + ('.format(dst = params[2], a = params[0], b = params[1]) carryStorage = prog.flags.getStorage(carryFlag) if type(carryStorage) is tuple: reg,bit = carryStorage reg = prog.resolveReg(reg, None, (), False) - check = '({reg} & 1 << {bit})'.format(reg=reg, bit=bit) + return '({reg} & 1 << {bit})'.format(reg=reg, bit=bit) else: - check = prog.resolveReg(carryStorage, None, (), False) - return base + check + ' ? 1 : 0);' + return prog.resolveReg(carryStorage, None, (), False) + +def _adcCImpl(prog, params, rawParams): + + return '\n\t{dst} = {a} + {b} + ({check} ? 1 : 0);'.format(dst = params[2], + a = params[0], b = params[1], check=_getCarryCheck(prog) + ) + +def _sbcCImpl(prog, params, rawParams): + return '\n\t{dst} = {a} - {b} - ({check} ? 1 : 0);'.format(dst = params[2], + a = params[0], b = params[1], check=_getCarryCheck(prog) + ) _opMap = { 'mov': Op(lambda val: val).cUnaryOperator(''), @@ -411,6 +420,7 @@ 'add': Op(lambda a, b: a + b).cBinaryOperator('+'), 'adc': Op().addImplementation('c', 2, _adcCImpl), 'sub': Op(lambda a, b: b - a).cBinaryOperator('-'), + 'sbc': Op().addImplementation('c', 2, _sbcCImpl), 'lsl': Op(lambda a, b: a << b).cBinaryOperator('<<'), 'lsr': Op(lambda a, b: a >> b).cBinaryOperator('>>'), 'asr': Op(lambda a, b: a >> b).addImplementation('c', 2, _asrCImpl),