OpenCores
URL https://opencores.org/ocsvn/a-z80/a-z80/trunk

Subversion Repositories a-z80

[/] [a-z80/] [trunk/] [tools/] [dongle/] [sbc/] [simulate-sub.py] - Rev 3

Go to most recent revision | Compare with Previous | Blame | View Log

#!/usr/bin/env python
#
# This script simulates 'sub' calculation and generates values for selected numbers.
# These can be compared with a real Z80 run values.
#
import sys
 
def printFlags(f):
    s = ''
    if f & (1 << 7):
        s += 'S'
    else:
        s += ' '
    if f & (1 << 6):
        s += 'Z'
    else:
        s += ' '
    if f & (1 << 5):
        s += 'Y'
    else:
        s += ' '
    if f & (1 << 4):
        s += 'H'
    else:
        s += ' '
    if f & (1 << 3):
        s += 'X'
    else:
        s += ' '
    if f & (1 << 2):
        s += 'P'
    else:
        s += ' '
    if f & (1 << 1):
        s += 'V'
    else:
        s += ' '
    if f & (1 << 0):
        s += 'C'
    else:
        s += ' '
    print 'Flags =                           %s' % s
 
def sbc(inA, op2, CYin):
    print '------------------------------------------'
    print 'Input: %0.2X SUB %0.2X' % ( inA, op2)
 
    cplA = inA ^ 0xFF               # Bit-wise complement of A
    CYin = 1
    finalA = cplA + 0 + CYin
    finalA = finalA + op2
 
    # Calculate CF while we have bit [9] available
    cf = 0
    if finalA > 255 or finalA < 0:
        cf = 1
 
    cf ^= 1                         # Complement CY since we used cpl(A) and not A
    nf = 1                          # 1 for SUB operation
 
    finalA = finalA & 0xFF          # Clamp final value to 8 bits
 
    #-------------------------------------------------------------------------------
    # Flag calculation: SF, ZF, YF, HF, XF, VF/PF, NF, CF
    #-------------------------------------------------------------------------------
    # Carry and Overflow calculation on Z80 require us to use internal carry-ins
    # http://stackoverflow.com/questions/8034566/overflow-and-carry-flags-on-z80
    carry_ins = finalA ^ inA ^ op2  # Bitfield of all internal carry-ins
 
    sf = (finalA>>7) & 1            # SF = Copy of [7]
    zf = finalA==0                  # ZF = Set if all result bits are zero
    yf = (finalA>>5) & 1            # YF = Copy of [5]
    hf = (carry_ins>>4)&1           # HF = Internal carry from bit [3] to [4]
    xf = (finalA>>3) & 1            # XF = Copy of [3]
    #                               # PF = XOR all final bits to get odd parity value
    pf = (((finalA>>7)^(finalA>>6)^(finalA>>5)^(finalA>>4)^(finalA>>3)^(finalA>>2)^(finalA>>1)^(finalA>>0))&1)^1
    vf = (carry_ins>>7)&1           # VF = Internal carry from bit [6] to [7]
    vf ^= cf                        # XOR'ed with the final carry out
 
    flags = (sf<<7) | (zf<<6) | (yf<<5) | (hf<<4) | (xf<<3) | (vf<<2) | (nf<<1) | (cf<<0)
 
    print 'Out:      A -> %0.2X    Flags = %0.2X' % ( finalA, flags)
    printFlags(flags)
 
sbc(0, 0, 0)
print 'Should be A -> 00    Flags = 42'
printFlags(0x42)
sbc(0, 1, 0)
print 'Should be A -> FF    Flags = BB'
printFlags(0xBB)
 
sbc(0xAA, 0x55, 0)
print 'Should be A -> 55    Flags = 06'
printFlags(0x06)
sbc(0x55, 0xAA, 0)
print 'Should be A -> AB    Flags = BF'
printFlags(0xBF)
 
sbc(0x0F, 0x03, 0)
print 'Should be A -> 0C    Flags = 0A'
printFlags(0x0A)
 

Go to most recent revision | Compare with Previous | Blame | View Log

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.