#!/usr/bin/python

"""
Globals used throughout the emulator.
"""


# Version of the emulator
PYMLAC_VERSION = 'pymlac 0.1'

# Some colours
BLACK = (0,0,0)
WHITE = (255,255,255)
YELLOW = (255,255,64)
GREY = (128,128,128)
LIGHTGREY = (182,182,182)
RED = (128, 0, 0)

# various screen size values
CANVAS_WIDTH = 1024
CANVAS_HEIGHT = 1024

# Position of version string on panel (in panel coordinates)
VERSION_POSN = (173,0)

# Positions of device boxes
BOX_POSNX = 5
BOX_POSNY = 45
BOX_OFFSETY = 42
BOX_WIDTH = 246
BOX_HEIGHT = 20
LABEL_OFFSETY = -19
EOF_OFFSETX = CANVAS_WIDTH + BOX_POSNX + 180
TAGS_OFFSETY = -17
ON_OFFSETX = 1254
OFF_OFFSETX = 1247
FILE_OFSETX = 1032
FILE_OFFSETY = +1

PTR_BOX_POSNY = BOX_POSNY
PTR_LABEL_POSN = (BOX_POSNX, PTR_BOX_POSNY + LABEL_OFFSETY)
PTR_EOF_POSN = (EOF_OFFSETX, PTR_BOX_POSNY + TAGS_OFFSETY)
PTR_ON_POSN = (ON_OFFSETX, PTR_BOX_POSNY + TAGS_OFFSETY)
PTR_OFF_POSN = (OFF_OFFSETX, PTR_BOX_POSNY + TAGS_OFFSETY)
PTR_FILE_POSN = (FILE_OFSETX, PTR_BOX_POSNY + FILE_OFFSETY)

PTP_BOX_POSNY = PTR_BOX_POSNY + BOX_OFFSETY
PTP_LABEL_POSN = (BOX_POSNX, PTP_BOX_POSNY + LABEL_OFFSETY)
PTP_EOF_POSN = (EOF_OFFSETX, PTP_BOX_POSNY + TAGS_OFFSETY)
PTP_ON_POSN = (ON_OFFSETX, PTP_BOX_POSNY + TAGS_OFFSETY)
PTP_OFF_POSN = (OFF_OFFSETX, PTP_BOX_POSNY + TAGS_OFFSETY)
PTP_FILE_POSN = (FILE_OFSETX, PTP_BOX_POSNY + FILE_OFFSETY)

TTYIN_BOX_POSNY = PTP_BOX_POSNY + BOX_OFFSETY
TTYIN_LABEL_POSN = (BOX_POSNX, TTYIN_BOX_POSNY + LABEL_OFFSETY)
TTYIN_EOF_POSN = (EOF_OFFSETX, TTYIN_BOX_POSNY + TAGS_OFFSETY)
TTYIN_ON_POSN = (ON_OFFSETX, TTYIN_BOX_POSNY + TAGS_OFFSETY)
TTYIN_OFF_POSN = (OFF_OFFSETX, TTYIN_BOX_POSNY + TAGS_OFFSETY)
TTYIN_FILE_POSN = (FILE_OFSETX, TTYIN_BOX_POSNY + FILE_OFFSETY)

TTYOUT_BOX_POSNY = TTYIN_BOX_POSNY + BOX_OFFSETY
TTYOUT_LABEL_POSN = (BOX_POSNX, TTYOUT_BOX_POSNY + LABEL_OFFSETY)
TTYOUT_EOF_POSN = (EOF_OFFSETX, TTYOUT_BOX_POSNY + TAGS_OFFSETY)
TTYOUT_ON_POSN = (ON_OFFSETX, TTYOUT_BOX_POSNY + TAGS_OFFSETY)
TTYOUT_OFF_POSN = (OFF_OFFSETX, TTYOUT_BOX_POSNY + TAGS_OFFSETY)
TTYOUT_FILE_POSN = (FILE_OFSETX, TTYOUT_BOX_POSNY + FILE_OFFSETY)

LEDAC_LABEL_OFFSETY = -18
LEDL_POSNX = 0
LEDAC_POSNX = 17
LEDAC_POSNY = 715
LEDPC_LABEL_OFFSETY = -18
LEDPC_POSNX = 17
LEDPC_POSNY = LEDAC_POSNY + 35
LED_BIT_OFFSETX = 15
LED_BIT_RANGE = (15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0)

LEDL_SCREEN_POSNX = CANVAS_WIDTH + LEDL_POSNX
LEDAC_SCREEN_POSNX = CANVAS_WIDTH + LEDAC_POSNX
LEDAC_SCREEN_POSNY = LEDAC_POSNY

LEDPC_SCREEN_POSNX = CANVAS_WIDTH + LEDPC_POSNX
LEDPC_SCREEN_POSNY = LEDPC_POSNY

PANEL_WIDTH = 256

REGS_MON_DIVIDERY = 205
ROM_MON_DIVIDERY = 285
FILE_ROM_DIVIDER = 690
MON_LED_DIVIDERY = 775
BOX_BOT_DIVIDERY = 875

BOX_POSNY = 802

REGL_BOX_POSNX = 73
REGL_BOX_POSNY = BOX_POSNY
REGL_BOX_WIDTH = 15
REGL_BOX_HEIGHT = 20

REGAC_BOX_POSNX = 95
REGAC_BOX_POSNY = REGL_BOX_POSNY
REGAC_BOX_WIDTH = 55
REGAC_BOX_HEIGHT = 20
REGPC_BOX_POSNX = 170
REGPC_BOX_POSNY = REGL_BOX_POSNY
REGPC_BOX_WIDTH = 55
REGPC_BOX_HEIGHT = 20

REGDX_BOX_POSNX = 33
REGDX_BOX_POSNY = BOX_POSNY + 40
REGDX_BOX_WIDTH = 55
REGDX_BOX_HEIGHT = 20

REGDY_BOX_POSNX = 95
REGDY_BOX_POSNY = REGDX_BOX_POSNY
REGDY_BOX_WIDTH = 55
REGDY_BOX_HEIGHT = 20

REGDPC_BOX_POSNX = 170
REGDPC_BOX_POSNY = REGDX_BOX_POSNY
REGDPC_BOX_WIDTH = 55
REGDPC_BOX_HEIGHT = 20

BOX_DATA_OFFSETX = 4
BOX_DATA_OFFSETY = 2

BOOTROM_POSNX = 10
BOOTROM_POSNY = 215

BOOTROM_LABEL_POSN = (BOOTROM_POSNX, BOOTROM_POSNY)
BOOTROM_LOADPTR_RADIO_POSN = (BOOTROM_POSNX + 5, BOOTROM_POSNY + 20)
BOOTROM_LOADPTR_LABEL_POSN = (BOOTROM_POSNX + 28, BOOTROM_POSNY + 18)
BOOTROM_LOADTTY_RADIO_POSN = (BOOTROM_POSNX + 5, BOOTROM_POSNY + 40)
BOOTROM_LOADTTY_LABEL_POSN = (BOOTROM_POSNX + 28, BOOTROM_POSNY + 40)
BOOTROM_WRITABLE_POSN = (BOOTROM_POSNX + 125, BOOTROM_POSNY + 20)
BOOTROM_WRITABLE_LABEL_POSN = (BOOTROM_POSNX + 148, BOOTROM_POSNY + 18)

SCREEN_BOOTROM_WRITABLE_POSN = (CANVAS_WIDTH + BOOTROM_POSNX + 125, BOOTROM_POSNY + 20)
SCREEN_BOOTROM_LOADPTR_RADIO_POSN = (CANVAS_WIDTH + BOOTROM_POSNX + 5, BOOTROM_POSNY + 20)
SCREEN_BOOTROM_LOADTTY_RADIO_POSN = (CANVAS_WIDTH + BOOTROM_POSNX + 5, BOOTROM_POSNY + 40)

QUITBUTTON_POSN = (142, 930)
HALTBUTTON_POSN = (14, 930)
RUNBUTTON_POSN = HALTBUTTON_POSN
SINGLESTEPBUTTON_POSN = (14, 890)

SCREEN_QUITBUTTON_POSN = (CANVAS_WIDTH + 142, 930)
SCREEN_HALTBUTTON_POSN = (CANVAS_WIDTH + 14, 930)
SCREEN_SINGLESTEPBUTTON_POSN = (CANVAS_WIDTH + 14, 890)

HALT_RECT = (SCREEN_HALTBUTTON_POSN, (100, 25))
QUIT_RECT = (SCREEN_QUITBUTTON_POSN, (100, 25))
SINGLESTEP_RECT = (SCREEN_SINGLESTEPBUTTON_POSN, (228, 25))
ROM_WRITABLE_RECT = (SCREEN_BOOTROM_WRITABLE_POSN, (19, 19))
LOADPTR_RADIO_RECT = (SCREEN_BOOTROM_LOADPTR_RADIO_POSN, (19, 19))
LOADTTY_RADIO_RECT = (SCREEN_BOOTROM_LOADTTY_RADIO_POSN, (19, 19))

# 'core' size (words) and save filename
CORE_FILENAME = 'pymlac.core'
MEMORY_SIZE = 040000	# 16K words memory size
PCMASK = MEMORY_SIZE - 1

# Trace stuff
TRACE_FILENAME = 'pymlac.trace'

# Number of emulator cycles per second
CYCLES_PER_SECOND = int(1000000 / 1.8)

# Definitions of boot ROM code type
ROM_PTR = 1
ROM_TTY = 2
ROM_NONE = 3

# The 4K 'local' mask
ADDRHIGHMASK = 0x7800

# word overflow and value masks
OVERFLOWMASK = 0xffff0000
WORDMASK = 0xffff
HIGHBITMASK = 0x8000
#ADDRMASK = 0x7fff
ADDRMASK = 037777

# global instruction cycle counter
#instruction_cycles = 0

# trace flag
tracing = 1

# logging flag
logging = 1

# A function to mask values to Imlac word width
def MASK_16(value):
    return value & 0xffff

# A function to mask addresses to Imlac max memory address
def MASK_MEM(address):
    return address & (MEMORY_SIZE - 1)

# A function to decide if an address is an auto-increment address
def ISAUTOINC(address):
    maskaddr = address & 0x7ff
    return (maskaddr >= 8) and (maskaddr <= 17)

