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

Subversion Repositories a-z80

[/] [a-z80/] [trunk/] [tools/] [dongle/] [sbc/] [simulate-sub.py] - Blame information for rev 3

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

Line No. Rev Author Line
1 3 gdevic
#!/usr/bin/env python
2
#
3
# This script simulates 'sub' calculation and generates values for selected numbers.
4
# These can be compared with a real Z80 run values.
5
#
6
import sys
7
 
8
def printFlags(f):
9
    s = ''
10
    if f & (1 << 7):
11
        s += 'S'
12
    else:
13
        s += ' '
14
    if f & (1 << 6):
15
        s += 'Z'
16
    else:
17
        s += ' '
18
    if f & (1 << 5):
19
        s += 'Y'
20
    else:
21
        s += ' '
22
    if f & (1 << 4):
23
        s += 'H'
24
    else:
25
        s += ' '
26
    if f & (1 << 3):
27
        s += 'X'
28
    else:
29
        s += ' '
30
    if f & (1 << 2):
31
        s += 'P'
32
    else:
33
        s += ' '
34
    if f & (1 << 1):
35
        s += 'V'
36
    else:
37
        s += ' '
38
    if f & (1 << 0):
39
        s += 'C'
40
    else:
41
        s += ' '
42
    print 'Flags =                           %s' % s
43
 
44
def sbc(inA, op2, CYin):
45
    print '------------------------------------------'
46
    print 'Input: %0.2X SUB %0.2X' % ( inA, op2)
47
 
48
    cplA = inA ^ 0xFF               # Bit-wise complement of A
49
    CYin = 1
50
    finalA = cplA + 0 + CYin
51
    finalA = finalA + op2
52
 
53
    # Calculate CF while we have bit [9] available
54
    cf = 0
55
    if finalA > 255 or finalA < 0:
56
        cf = 1
57
 
58
    cf ^= 1                         # Complement CY since we used cpl(A) and not A
59
    nf = 1                          # 1 for SUB operation
60
 
61
    finalA = finalA & 0xFF          # Clamp final value to 8 bits
62
 
63
    #-------------------------------------------------------------------------------
64
    # Flag calculation: SF, ZF, YF, HF, XF, VF/PF, NF, CF
65
    #-------------------------------------------------------------------------------
66
    # Carry and Overflow calculation on Z80 require us to use internal carry-ins
67
    # http://stackoverflow.com/questions/8034566/overflow-and-carry-flags-on-z80
68
    carry_ins = finalA ^ inA ^ op2  # Bitfield of all internal carry-ins
69
 
70
    sf = (finalA>>7) & 1            # SF = Copy of [7]
71
    zf = finalA==0                  # ZF = Set if all result bits are zero
72
    yf = (finalA>>5) & 1            # YF = Copy of [5]
73
    hf = (carry_ins>>4)&1           # HF = Internal carry from bit [3] to [4]
74
    xf = (finalA>>3) & 1            # XF = Copy of [3]
75
    #                               # PF = XOR all final bits to get odd parity value
76
    pf = (((finalA>>7)^(finalA>>6)^(finalA>>5)^(finalA>>4)^(finalA>>3)^(finalA>>2)^(finalA>>1)^(finalA>>0))&1)^1
77
    vf = (carry_ins>>7)&1           # VF = Internal carry from bit [6] to [7]
78
    vf ^= cf                        # XOR'ed with the final carry out
79
 
80
    flags = (sf<<7) | (zf<<6) | (yf<<5) | (hf<<4) | (xf<<3) | (vf<<2) | (nf<<1) | (cf<<0)
81
 
82
    print 'Out:      A -> %0.2X    Flags = %0.2X' % ( finalA, flags)
83
    printFlags(flags)
84
 
85
sbc(0, 0, 0)
86
print 'Should be A -> 00    Flags = 42'
87
printFlags(0x42)
88
sbc(0, 1, 0)
89
print 'Should be A -> FF    Flags = BB'
90
printFlags(0xBB)
91
 
92
sbc(0xAA, 0x55, 0)
93
print 'Should be A -> 55    Flags = 06'
94
printFlags(0x06)
95
sbc(0x55, 0xAA, 0)
96
print 'Should be A -> AB    Flags = BF'
97
printFlags(0xBF)
98
 
99
sbc(0x0F, 0x03, 0)
100
print 'Should be A -> 0C    Flags = 0A'
101
printFlags(0x0A)

powered by: WebSVN 2.1.0

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