OpenCores
URL https://opencores.org/ocsvn/2d_game_console/2d_game_console/trunk

Subversion Repositories 2d_game_console

[/] [2d_game_console/] [trunk/] [test_code_generator_0715.py] - Rev 2

Compare with Previous | Blame | View Log

#Author: Lucas Alves
#License: Public Domain
#Assembly Test Code Generator
 
#import string
import sys
 
from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
import os
import math
 
results_list = []
 
color_dict = {'RED':31, 'GREEN':2016 ,'BLUE':-2048, 'YELLOW':2047, 'MAGENTA':-2017, 'CYAN':-32, 'GRAY':-31728, 'BLACK': 0,
              'WHITE':-1, 'BROWN':10580, 'PURPLE':-32752, 'ORANGE':1343, 'PINK':-12769, 'GOLD':1727, 'SILVER':-14824, 'OLIVE':1040}
 
class GUI:
 
    # Contructor
    def __init__(self):
        self.root = Tk()
        self.root.title("Assembly Test Code Generator")
        self.root.resizable(False, False)
 
        fileLabelFrame = Frame(self.root)
        fileLabelFrame.pack(fill=BOTH)
 
        fileLabel = Label(fileLabelFrame, text="Assembly file:")
        fileLabel.pack(side=LEFT, padx=5, pady=0)
 
        fileFrame = Frame(self.root)
        fileFrame.pack(fill=BOTH)
 
        self.fileEntry = Entry(fileFrame, width=50)
        self.fileEntry.pack(side=LEFT, padx=5, pady=10)
 
        browseButton = Button(fileFrame, text="Save As...", fg="black", command=self.ask_save_file)
        browseButton.pack(side=LEFT, padx=5, pady=0)
 
        self.root.mainloop()
 
    def ask_save_file(self):
        currdir = os.getcwd()
        self.filenameSaved = filedialog.asksaveasfilename(parent=self.root, initialdir=currdir, title='Save file',
                                                          defaultextension='.asm', filetypes = [("Assembly File","*.asm")])
        if len(self.filenameSaved) > 0:
            self.fileEntry.delete(0, END)
            self.fileEntry.insert(0, self.filenameSaved)
            self.execute()
 
    def quit(self):
        self.root.destroy()
        sys.exit()
 
    def execute(self):
        self.mif_file = open(self.filenameSaved, 'w')
        address = 1032                                          # Memory start address for data storage
        values_list = [0, -1, 1, -21846, 21845, 32767, -32768]  # Registers values
 
 
        self.mif_file.write('BEGIN:\n')
        self.mif_file.write('\tLIMM R0, 0     ' + '     ' + '// R0 <- 0000000000000000 (=0) \n')
        self.mif_file.write('\tLIMM R1, 0     ' + '     ' + '// R1 <- 0000000000000000 (=0) \n')
        self.mif_file.write('\tLIMM R2, -1    ' + '     ' + '// R2 <- 1111111111111111 (=-1) \n')
        self.mif_file.write('\tLIMM R3, 1     ' + '     ' + '// R3 <- 0000000000000001 (=1) \n')
        self.mif_file.write('\tLIMM R4, -21846' + '     ' + '// R4 <- 1010101010101010 (=-21846) \n')
        self.mif_file.write('\tLIMM R5, 21845 ' + '     ' + '// R5 <- 0101010101010101 (=21845) \n')
        self.mif_file.write('\tLIMM R6, 32767 ' + '     ' + '// R6 <- 0111111111111111 (=32767) \n')
        self.mif_file.write('\tLIMM R7, -32768' + '     ' + '// R7 <- 1000000000000000 (=-32768) \n')
 
        self.mif_file.write('\n')
 
        # SPRITES
        first_row = 33
        last_row = 512
        first_column = 48
        last_column = 687
 
        for index in range(0, 16):
            color_values = list(color_dict.values())
            color = color_values[index]
 
            self.mif_file.write('SPRITE_LEVEL_' + str(index) + ':\n')
 
            # SPRITE LEVEL
            self.mif_file.write('\tLIMM R28, {:<6}'.format(index) )
            self.mif_file.write('\t\t\t// R28 <- ')
            self.mif_file.write(str(index) + ' (Sprite Level ' + str(index) + ')\n')
 
            if (index < 8):
                row = first_row+20
                column = first_column+20 + (index * 20)
 
                # SPRITE ROW
                self.mif_file.write('\tLIMM R1' + str(index) + ', {:<6}'.format(row) )
                self.mif_file.write('\t\t\t// R1' + str(index) + ' <- ')
                self.mif_file.write(str(row) + ' (Sprite Level ' + str(index) + ' row value = ' + str(row) + ')\n')
 
                # SPRITE COLUMN
                self.mif_file.write('\tLIMM R2' + str(index) + ', {:<6}'.format(column) )
                self.mif_file.write('\t\t\t// R2' + str(index) + ' <- ')
                self.mif_file.write(str(column) + ' (Sprite Level ' + str(index) + ' column value = ' + str(column) + ')\n')
 
                # SPRITE POSITION
                self.mif_file.write('\tSPRITE_POS R28, R1' + str(index) + ', R2' + str(index))
                self.mif_file.write('\t// SPRITE_POS LEVEL, ROW, COLUMN\n')
 
                self.mif_file.write('\n')
 
            else:
                if (index == 8):
                    row = 265
                    column = 360
 
                if (index == 9):
                    row = first_row + 1
                    column = first_column + 1
 
                if (index == 10):
                    row = first_row + 1
                    column = last_column - 16
 
                if (index == 11):
                    row = last_row - 16
                    column = first_column + 1
 
                if (index == 12):
                    row = last_row - 16
                    column = last_column - 16
 
                if ( (index >= 13) and (index < 16) ):
                    row = first_row + 40
                    column = first_column+20 + ((index-13) * 20)
 
                # SPRITE ROW
                self.mif_file.write('\tLIMM R9, ' + '{:<6}'.format(row) )
                self.mif_file.write('\t\t\t\t// R9 <- ')
                self.mif_file.write(str(row) + ' (Sprite Level ' + str(index) + ' row value = ' + str(row) + ')\n')
 
                # SPRITE COLUMN
                self.mif_file.write('\tLIMM R29, {:<6}'.format(column) )
                self.mif_file.write('\t\t\t// R29 <- ')
                self.mif_file.write(str(column) + ' (Sprite Level ' + str(index) + ' column value = ' + str(column) + ')\n')
 
                # SPRITE POSITION
                self.mif_file.write('\tSPRITE_POS R28, R9, R29')
                self.mif_file.write('\t\t// SPRITE_POS LEVEL, ROW, COLUMN\n')
 
                self.mif_file.write('\n')
 
            # SPRITE ID
            self.mif_file.write('\tLIMM R29, {:<6}'.format(index) )
            self.mif_file.write('\t\t\t// R29 <- ')
            self.mif_file.write(str(index) + ' (Sprite ID ' + str(index) + ')\n')
 
            self.mif_file.write('\tSPRITE_ID R28, R29')
            self.mif_file.write('\t\t\t// SPRITE_ID LEVEL, ID\n')
 
            self.mif_file.write('\n')
 
            # SPRITE COLOR
            self.mif_file.write('\tLIMM R29, {:<6}'.format(color) )
            self.mif_file.write('\t\t\t// R29 <- ')
            self.mif_file.write(str(color) + ' (Sprite Color ' + str(color) + ')\n')
 
            self.mif_file.write('\tSPRITE_COLOR R28, R29')
            self.mif_file.write('\t\t// SPRITE_COLOR LEVEL, COLOR\n')
 
            self.mif_file.write('\n')
 
 
        # LOOP START
        self.mif_file.write('START:\n')
        self.mif_file.write('\tLIMM R0, 0     ' + '     ' + '// R0 <- 0000000000000000 (=0) \n')
        self.mif_file.write('\tLIMM R1, 0     ' + '     ' + '// R1 <- 0000000000000000 (=0) \n')
        self.mif_file.write('\tLIMM R2, -1    ' + '     ' + '// R2 <- 1111111111111111 (=-1) \n')
        self.mif_file.write('\tLIMM R3, 1     ' + '     ' + '// R3 <- 0000000000000001 (=1) \n')
        self.mif_file.write('\tLIMM R4, -21846' + '     ' + '// R4 <- 1010101010101010 (=-21846) \n')
        self.mif_file.write('\tLIMM R5, 21845 ' + '     ' + '// R5 <- 0101010101010101 (=21845) \n')
        self.mif_file.write('\tLIMM R6, 32767 ' + '     ' + '// R6 <- 0111111111111111 (=32767) \n')
        self.mif_file.write('\tLIMM R7, -32768' + '     ' + '// R7 <- 1000000000000000 (=-32768) \n')
 
        self.mif_file.write('\n')
 
        self.mif_file.write('\t//Enable all interrupts\n')
        self.mif_file.write('\tSW R2, 1024 (R0)')
        self.mif_file.write('\t// ADDR 1024: <- 1111111111111111 (=-1)\n')
 
        self.mif_file.write('\n')
 
        self.mif_file.write('\t//Move Sprite 7\n')
        # SPRITE LEVEL 7
        self.mif_file.write('\tLIMM R28, {:<6}'.format(7) )
        self.mif_file.write('\t\t\t// R28 <- ')
        self.mif_file.write(str(7) + ' (Sprite Level ' + str(7) + ')\n')
        # INCREMENT ROW POSITION
        self.mif_file.write('\tADD R17, R3')
        self.mif_file.write('\t\t\t\t\t// R17 + R3 = R17 + 1\n')
        # INCREMENT COLUMN POSITION
        self.mif_file.write('\tADD R27, R3')
        self.mif_file.write('\t\t\t\t\t// R27 + R3 = R27 + 1\n')
        # SPRITE POSITION
        self.mif_file.write('\tSPRITE_POS R28, R17, R27')
        self.mif_file.write('\t// SPRITE_POS LEVEL, ROW, COLUMN\n')
 
        self.mif_file.write('\n')
 
        # ADD
        for reg_a in range(1, 8):
            self.mif_file.write('\nADD_R' + str(reg_a) + ':\n')                                             # Change operation (ADD, SUB, etc)
            for reg_b in range(1, 8):
                address = address + 1
                result = values_list[reg_a-1] + values_list[reg_b-1]                                        # Change sign (+, -, etc)
                if ( (result > 32767) or (result < -32768) ):
                    results_list.append(-1)
                else:
                    results_list.append(result)
 
                self.mif_file.write('\tADD R' + str(reg_a) + ', R' + '{:<7}'.format(reg_b) )                # Change operation (ADD, SUB, etc)
                self.mif_file.write('\t// R' + str(reg_a) + ' + R' + str(reg_b) + ' = ' + str(result))      # Change sign (+, -, etc)
                if ( (result > 32767) or (result < -32768) ):
                    self.mif_file.write(' (R' + str(reg_a) + ' <- -1' + ') !!!!!OVERFLOW!!!!!\n')
                else:
                    self.mif_file.write(' (R' + str(reg_a) + ' <- ' + str(result) + ') \n')
 
                self.mif_file.write('\tSW R' + str(reg_a) + ', ' + str(address) + ' (R0)')
                if ( (result > 32767) or (result < -32768) ):
                    self.mif_file.write('\t// ADDR ' + str(address) + ': <- -1' + '\n')
                else:
                    self.mif_file.write('\t// ADDR ' + str(address) + ': <- ' + str(result) + '\n')
 
                self.mif_file.write('\tLIMM R' + str(reg_a) + ', ' + '{:<6}'.format(values_list[reg_a-1]) )
                self.mif_file.write('\t\t// R' + str(reg_a) + ' <- ' + str(values_list[reg_a-1]) + '\n')
 
                if ( (result > 32767) or (result < -32768) ):
                    self.mif_file.write('\tBRFL ERROR, 6, 0')
                    self.mif_file.write('\t// BRANCH TO ERROR IF RFLAGS[6] = 0\n')
                else:
                    self.mif_file.write('\tBRFL ERROR, 6, 1')
                    self.mif_file.write('\t// BRANCH TO ERROR IF RFLAGS[6] = 1\n')
 
                self.mif_file.write('\n')
 
 
        # SUB
        for reg_a in range(1, 8):
            self.mif_file.write('\nSUB_R' + str(reg_a) + ':\n')                                             # Change operation (ADD, SUB, etc)
            for reg_b in range(1, 8):
                address = address + 1
                result = values_list[reg_a-1] - values_list[reg_b-1]                                        # Change sign (+, -, etc)
                if ( (result > 32767) or (result < -32768) ):
                    results_list.append(-1)
                else:
                    results_list.append(result)
 
                self.mif_file.write('\tSUB R' + str(reg_a) + ', R' + '{:<7}'.format(reg_b) )                # Change operation (ADD, SUB, etc)
                self.mif_file.write('\t// R' + str(reg_a) + ' - R' + str(reg_b) + ' = ' + str(result))      # Change sign (+, -, etc)
                if ( (result > 32767) or (result < -32768) ):
                    self.mif_file.write(' (R' + str(reg_a) + ' <- -1' + ') !!!!!OVERFLOW!!!!!\n')
                else:
                    self.mif_file.write(' (R' + str(reg_a) + ' <- ' + str(result) + ') \n')
 
                self.mif_file.write('\tSW R' + str(reg_a) + ', ' + str(address) + ' (R0)')
                if ( (result > 32767) or (result < -32768) ):
                    self.mif_file.write('\t// ADDR ' + str(address) + ': <- -1' + '\n')
                else:
                    self.mif_file.write('\t// ADDR ' + str(address) + ': <- ' + str(result) + '\n')
 
                self.mif_file.write('\tLIMM R' + str(reg_a) + ', ' + '{:<6}'.format(values_list[reg_a-1]) )
                self.mif_file.write('\t\t// R' + str(reg_a) + ' <- ' + str(values_list[reg_a-1]) + '\n')
 
                if ( (result > 32767) or (result < -32768) ):
                    self.mif_file.write('\tBRFL ERROR, 6, 0')
                    self.mif_file.write('\t// BRANCH TO ERROR IF RFLAGS[6] = 0\n')
                else:
                    self.mif_file.write('\tBRFL ERROR, 6, 1')
                    self.mif_file.write('\t// BRANCH TO ERROR IF RFLAGS[6] = 1\n')
 
                self.mif_file.write('\n')
 
 
        # MUL
        for reg_a in range(1, 8):
            self.mif_file.write('\nMUL_R' + str(reg_a) + ':\n')                                             # Change operation (ADD, SUB, etc)
            for reg_b in range(1, 8):
                address = address + 1
                result = values_list[reg_a-1] * values_list[reg_b-1]                                        # Change sign (+, -, etc)
                if ( (result > 32767) or (result < -32768) ):
                    results_list.append(-1)
                else:
                    results_list.append(result)
 
                self.mif_file.write('\tMUL R' + str(reg_a) + ', R' + '{:<7}'.format(reg_b) )                # Change operation (ADD, SUB, etc)
                self.mif_file.write('\t// R' + str(reg_a) + ' * R' + str(reg_b) + ' = ' + str(result))      # Change sign (+, -, etc)
                if ( (result > 32767) or (result < -32768) ):
                    self.mif_file.write(' (R' + str(reg_a) + ' <- -1' + ') !!!!!OVERFLOW!!!!!\n')
                else:
                    self.mif_file.write(' (R' + str(reg_a) + ' <- ' + str(result) + ') \n')
 
                self.mif_file.write('\tSW R' + str(reg_a) + ', ' + str(address) + ' (R0)')
                if ( (result > 32767) or (result < -32768) ):
                    self.mif_file.write('\t// ADDR ' + str(address) + ': <- -1' + '\n')
                else:
                    self.mif_file.write('\t// ADDR ' + str(address) + ': <- ' + str(result) + '\n')
 
                self.mif_file.write('\tLIMM R' + str(reg_a) + ', ' + '{:<6}'.format(values_list[reg_a-1]) )
                self.mif_file.write('\t\t// R' + str(reg_a) + ' <- ' + str(values_list[reg_a-1]) + '\n')
 
                if ( (result > 32767) or (result < -32768) ):
                    self.mif_file.write('\tBRFL ERROR, 6, 0')
                    self.mif_file.write('\t// BRANCH TO ERROR IF RFLAGS[6] = 0\n')
                else:
                    self.mif_file.write('\tBRFL ERROR, 6, 1')
                    self.mif_file.write('\t// BRANCH TO ERROR IF RFLAGS[6] = 1\n')
 
                self.mif_file.write('\n')
 
 
        # DIV
        for reg_a in range(1, 8):
            self.mif_file.write('\nDIV_R' + str(reg_a) + ':\n')                                             # Change operation (ADD, SUB, etc)
            for reg_b in range(1, 8):
                address = address + 1
 
                if (values_list[reg_b-1] != 0):
                    result = values_list[reg_a-1] / values_list[reg_b-1]                                    # Change sign (+, -, etc)
 
                    if (values_list[reg_b-1] > 0):
                        result = math.floor(result)
                    else:
                        result = math.ceil(result)
                else:
                    result = 9999999999
 
                if ( (result > 32767) or (result < -32768) ):
                    results_list.append(-1)                                                                 # Division by 0 and Overflow, result = -1
                else:
                    results_list.append(result)
 
                # DIVIDE
                self.mif_file.write('\tDIV R' + str(reg_a) + ', R' + '{:<7}'.format(reg_b) )                # Change operation (ADD, SUB, etc)
                self.mif_file.write('\t// ' + str(values_list[reg_a-1]) + ' / ' + str(values_list[reg_b-1]) + ' = ' + str(result))      # Change sign (+, -, etc)
 
                if (result == 9999999999):
                    self.mif_file.write(' (R' + str(reg_a) + ' <- -1' + ') !!!!!DIV BY 0!!!!!\n')
                else:
                    if ( (result > 32767) or (result < -32768) ):
                        self.mif_file.write(' (R' + str(reg_a) + ' <- -1' + ') !!!!!OVERFLOW!!!!!\n')
                    else:
                        self.mif_file.write(' (R' + str(reg_a) + ' <- ' + str(result) + ') \n')
 
                # STORE WORD
                self.mif_file.write('\tSW R' + str(reg_a) + ', ' + str(address) + ' (R0)')
                if ( (result > 32767) or (result < -32768) ):
                    self.mif_file.write('\t// ADDR ' + str(address) + ': <- -1' + '\n')
                else:
                    self.mif_file.write('\t// ADDR ' + str(address) + ': <- ' + str(result) + '\n')
 
                # LOAD IMMEDIATE
                self.mif_file.write('\tLIMM R' + str(reg_a) + ', ' + '{:<6}'.format(values_list[reg_a-1]) )
                self.mif_file.write('\t\t// R' + str(reg_a) + ' <- ' + str(values_list[reg_a-1]) + '\n')
 
                # BRANCH
                if (result == 9999999999):
                    self.mif_file.write('\tBRFL ERROR, 0, 0')
                    self.mif_file.write('\t// BRANCH TO ERROR IF RFLAGS[0] = 0\n')
 
                    self.mif_file.write('\tBRFL ERROR, 6, 1')
                    self.mif_file.write('\t// BRANCH TO ERROR IF RFLAGS[6] = 1\n')
                else:
                    if ( (result > 32767) or (result < -32768) ):
                        self.mif_file.write('\tBRFL ERROR, 0, 1')
                        self.mif_file.write('\t// BRANCH TO ERROR IF RFLAGS[0] = 1\n')
 
                        self.mif_file.write('\tBRFL ERROR, 6, 0')
                        self.mif_file.write('\t// BRANCH TO ERROR IF RFLAGS[6] = 0\n')
                    else:
                        self.mif_file.write('\tBRFL ERROR, 0, 1')
                        self.mif_file.write('\t// BRANCH TO ERROR IF RFLAGS[0] = 1\n')
 
                        self.mif_file.write('\tBRFL ERROR, 6, 1')
                        self.mif_file.write('\t// BRANCH TO ERROR IF RFLAGS[6] = 1\n')
 
                self.mif_file.write('\n')
 
 
        # AND
        for reg_a in range(1, 8):
            self.mif_file.write('\nAND_R' + str(reg_a) + ':\n')                                                     # Change operation (ADD, SUB, etc)
            for reg_b in range(1, 8):
                address = address + 1
                result = values_list[reg_a-1] & values_list[reg_b-1]                                                # Change sign (+, -, etc)
                results_list.append(result)
 
                self.mif_file.write('\t// ' + '{0:016b}'.format(values_list[reg_a-1] & 0b1111111111111111)
                                    + ' <- R' + str(reg_a) + ' = ' + str(values_list[reg_a-1]) + '\n' )
                self.mif_file.write('\t// ' + '{0:016b}'.format(values_list[reg_b-1] & 0b1111111111111111)
                                    + ' <- R' + str(reg_b) + ' = ' + str(values_list[reg_b-1]) + '\n' )
                self.mif_file.write('\t// ' + '{0:016b}'.format(result & 0b1111111111111111)
                                    + ' <- R' + str(reg_a) + ' & R' + str(reg_b) + ' = ' + str(result) + '\n' )     # Change sign (+, -, etc)
 
                self.mif_file.write('\tAND R' + str(reg_a) + ', R' + '{:<7}'.format(reg_b) )                        # Change operation (ADD, SUB, etc)
                self.mif_file.write('\t// (R' + str(reg_a) + ' <- ' + str(result) + ') \n')
 
                self.mif_file.write('\tSW R' + str(reg_a) + ', ' + str(address) + ' (R0)')
                self.mif_file.write('\t// ADDR ' + str(address) + ': <- ' + str(result) + '\n')
 
                self.mif_file.write('\tLIMM R' + str(reg_a) + ', ' + '{:<6}'.format(values_list[reg_a-1]) )
                self.mif_file.write('\t\t// R' + str(reg_a) + ' <- ' + str(values_list[reg_a-1]) + '\n' )
 
                self.mif_file.write('\n')
 
 
        # OR
        for reg_a in range(1, 8):
            self.mif_file.write('\nOR_R' + str(reg_a) + ':\n')                                                      # Change operation (ADD, SUB, etc)
            for reg_b in range(1, 8):
                address = address + 1
                result = values_list[reg_a-1] | values_list[reg_b-1]                                                # Change sign (+, -, etc)
                results_list.append(result)
 
                self.mif_file.write('\t// ' + '{0:016b}'.format(values_list[reg_a-1] & 0b1111111111111111)
                                    + ' <- R' + str(reg_a) + ' = ' + str(values_list[reg_a-1]) + '\n' )
                self.mif_file.write('\t// ' + '{0:016b}'.format(values_list[reg_b-1] & 0b1111111111111111)
                                    + ' <- R' + str(reg_b) + ' = ' + str(values_list[reg_b-1]) + '\n' )
                self.mif_file.write('\t// ' + '{0:016b}'.format(result & 0b1111111111111111)
                                    + ' <- R' + str(reg_a) + ' | R' + str(reg_b) + ' = ' + str(result) + '\n' )     # Change sign (+, -, etc)
 
                self.mif_file.write('\tOR R' + str(reg_a) + ', R' + '{:<8}'.format(reg_b) )                         # Change operation (ADD, SUB, etc)
                self.mif_file.write('\t// (R' + str(reg_a) + ' <- ' + str(result) + ') \n')
 
                self.mif_file.write('\tSW R' + str(reg_a) + ', ' + str(address) + ' (R0)')
                self.mif_file.write('\t// ADDR ' + str(address) + ': <- ' + str(result) + '\n')
 
                self.mif_file.write('\tLIMM R' + str(reg_a) + ', ' + '{:<6}'.format(values_list[reg_a-1]) )
                self.mif_file.write('\t\t// R' + str(reg_a) + ' <- ' + str(values_list[reg_a-1]) + '\n' )
 
                self.mif_file.write('\n')
 
 
        # NOT
        for reg_a in range(1, 8):
            self.mif_file.write('\nNOT_R' + str(reg_a) + ':\n')                                                     # Change operation (ADD, SUB, etc)
 
            address = address + 1
            result = ~ values_list[reg_a-1]                                                                         # Change sign (+, -, etc)
            results_list.append(result)
 
            self.mif_file.write('\t// ' + '{0:016b}'.format(values_list[reg_a-1] & 0b1111111111111111)
                                + ' <- R' + str(reg_a) + ' = ' + str(values_list[reg_a-1]) + '\n' )
            self.mif_file.write('\t// ' + '{0:016b}'.format(result & 0b1111111111111111)
                                + ' <- ~R' + str(reg_a) + ' = ' + str(result) + '\n' )                              # Change sign (+, -, etc)
 
            self.mif_file.write('\tNOT R' + '{:<11}'.format(reg_a) )                                                # Change operation (ADD, SUB, etc)
            self.mif_file.write('\t// (R' + str(reg_a) + ' <- ' + str(result) + ') \n')
 
            self.mif_file.write('\tSW R' + str(reg_a) + ', ' + str(address) + ' (R0)')
            self.mif_file.write('\t// ADDR ' + str(address) + ': <- ' + str(result) + '\n')
 
            self.mif_file.write('\tLIMM R' + str(reg_a) + ', ' + '{:<6}'.format(values_list[reg_a-1]) )
            self.mif_file.write('\t\t// R' + str(reg_a) + ' <- ' + str(values_list[reg_a-1]) + '\n' )
 
            self.mif_file.write('\n')
 
 
        # CMP
        for index, results_list_value in enumerate(results_list):
            address = 1033+index
            self.mif_file.write('\nCMP_' + str(address) + ':\n')                                                    # Change operation (ADD, SUB, etc)
 
            result = results_list_value                                                                             # Change sign (+, -, etc)
 
            self.mif_file.write('\tLW R31, ' + str(address) + ' (R0)')
            self.mif_file.write('\t// R31 <- ' + str(result) + ' (ADDR: ' + str(address) + ')' + '\n')
 
            # R31 = R30 = RESULT
            self.mif_file.write('\n\tLIMM R30, ' + '{:<6}'.format(result) )
            self.mif_file.write('\t// R30 <- ' + str(result) + '\n' )
 
            self.mif_file.write('\tCMP R30, R31\n')                                                             # Change operation (ADD, SUB, etc)
 
            self.mif_file.write('\tBRFL ERROR, 3, 1')
            self.mif_file.write('\t// BRANCH TO ERROR IF RFLAGS[3] = 1 (BELOW=1)\n')      #BELOW
 
            self.mif_file.write('\tBRFL ERROR, 4, 0')
            self.mif_file.write('\t// BRANCH TO ERROR IF RFLAGS[4] = 0 (EQUAL=0)\n')      #EQUAL
 
            self.mif_file.write('\tBRFL ERROR, 5, 1')
            self.mif_file.write('\t// BRANCH TO ERROR IF RFLAGS[5] = 1 (ABOVE=1)\n')      #ABOVE
 
            # R31 > R30 = RESULT-1
            if (result > -32768):
                self.mif_file.write('\n\tLIMM R30, ' + '{:<6}'.format(result-1) )
                self.mif_file.write('\t// R30 <- ' + str(result-1) + '\n' )
 
                self.mif_file.write('\tCMP R30, R31\n')                                                             # Change operation (ADD, SUB, etc)
 
                self.mif_file.write('\tBRFL ERROR, 3, 0')
                self.mif_file.write('\t// BRANCH TO ERROR IF RFLAGS[3] = 0 (BELOW=0)\n')      #BELOW
 
                self.mif_file.write('\tBRFL ERROR, 4, 1')
                self.mif_file.write('\t// BRANCH TO ERROR IF RFLAGS[4] = 1 (EQUAL=1)\n')      #EQUAL
 
                self.mif_file.write('\tBRFL ERROR, 5, 1')
                self.mif_file.write('\t// BRANCH TO ERROR IF RFLAGS[5] = 1 (ABOVE=1)\n')      #ABOVE
 
            # R31 < R30 = RESULT+1
            if (result < 32767):
                self.mif_file.write('\n\tLIMM R30, ' + '{:<6}'.format(result+1) )
                self.mif_file.write('\t// R30 <- ' + str(result+1) + '\n' )
 
                self.mif_file.write('\tCMP R30, R31\n')                                                             # Change operation (ADD, SUB, etc)
 
                self.mif_file.write('\tBRFL ERROR, 3, 1')
                self.mif_file.write('\t// BRANCH TO ERROR IF RFLAGS[3] = 1 (BELOW=1)\n')      #BELOW
 
                self.mif_file.write('\tBRFL ERROR, 4, 1')
                self.mif_file.write('\t// BRANCH TO ERROR IF RFLAGS[4] = 1 (EQUAL=1)\n')      #EQUAL
 
                self.mif_file.write('\tBRFL ERROR, 5, 0')
                self.mif_file.write('\t// BRANCH TO ERROR IF RFLAGS[5] = 0 (ABOVE=0)\n')      #ABOVE
 
 
            self.mif_file.write('\n')
 
 
        self.mif_file.write('SUCCESS:\n')
        self.mif_file.write('\tJMP START\n\n')
 
 
        self.mif_file.write('ERROR:\n')
        self.mif_file.write('\tJMP ERROR\n\n')
 
 
        # Interruption Service Routine 0
        self.mif_file.write('.ISR0:\n')
 
        # Load buttons values
        self.mif_file.write('\tLW R8, 1025 (R0)')
        self.mif_file.write('\t\t// R8 <- BUTTONS\n\n')
 
        # Buttons values
        UP = 1
        DOWN = 2
        LEFT = 4
        RIGHT = 8
        B = 16
        C = 32
        A = 64
        START = 128
        Z = 256
        Y = 512
        X = 1024
        MODE = 2048
 
        # Image limits
        start_row = 33
        end_row = 512
        start_col = 48
        end_col = 687
 
        check_buttons_list = [(UP+A),       (DOWN+A),       (LEFT+A),       (RIGHT+A),
                              (UP+B),       (DOWN+B),       (LEFT+B),       (RIGHT+B),
                              (UP+C),       (DOWN+C),       (LEFT+C),       (RIGHT+C),
                              (UP+X),       (DOWN+X),       (LEFT+X),       (RIGHT+X),
                              (UP+Y),       (DOWN+Y),       (LEFT+Y),       (RIGHT+Y),
                              (UP+Z),       (DOWN+Z),       (LEFT+Z),       (RIGHT+Z),
                              (UP+START),   (DOWN+START),   (LEFT+START),   (RIGHT+START),
                              (UP+MODE),    (DOWN+MODE),    (LEFT+MODE),    (RIGHT+MODE)]
 
        action_labels_list = ['UP_A',       'DOWN_A',       'LEFT_A',       'RIGHT_A',
                              'UP_B',       'DOWN_B',       'LEFT_B',       'RIGHT_B',
                              'UP_C',       'DOWN_C',       'LEFT_C',       'RIGHT_C',
                              'UP_X',       'DOWN_X',       'LEFT_X',       'RIGHT_X',
                              'UP_Y',       'DOWN_Y',       'LEFT_Y',       'RIGHT_Y',
                              'UP_Z',       'DOWN_Z',       'LEFT_Z',       'RIGHT_Z',
                              'UP_START',   'DOWN_START',   'LEFT_START',   'RIGHT_START',
                              'UP_MODE',    'DOWN_MODE',    'LEFT_MODE',    'RIGHT_MODE']
 
        return_labels_list = ['RETURN_UP_A',        'RETURN_DOWN_A',        'RETURN_LEFT_A',        'RETURN_RIGHT_A',
                              'RETURN_UP_B',        'RETURN_DOWN_B',        'RETURN_LEFT_B',        'RETURN_RIGHT_B',
                              'RETURN_UP_C',        'RETURN_DOWN_C',        'RETURN_LEFT_C',        'RETURN_RIGHT_C',
                              'RETURN_UP_X',        'RETURN_DOWN_X',        'RETURN_LEFT_X',        'RETURN_RIGHT_X',
                              'RETURN_UP_Y',        'RETURN_DOWN_Y',        'RETURN_LEFT_Y',        'RETURN_RIGHT_Y',
                              'RETURN_UP_Z',        'RETURN_DOWN_Z',        'RETURN_LEFT_Z',        'RETURN_RIGHT_Z',
                              'RETURN_UP_START',    'RETURN_DOWN_START',    'RETURN_LEFT_START',    'RETURN_RIGHT_START',
                              'RETURN_UP_MODE',     'RETURN_DOWN_MODE',     'RETURN_LEFT_MODE',     'RETURN_RIGHT_MODE']
 
 
        # Check buttons and branch
        for index, buttons_value in enumerate(check_buttons_list):
 
            self.mif_file.write('\t// CHECK ' + action_labels_list[index] + '\n')
 
            self.mif_file.write('\tLIMM R18, ' + '{:<6}'.format(buttons_value) )
            self.mif_file.write('\t\t// R18 <- ')
            self.mif_file.write('{0:016b}'.format(buttons_value & 0b1111111111111111) + ' (Value for ' + action_labels_list[index] + ')\n')
 
            self.mif_file.write('\tLIMM R19, ' + '{:<6}'.format(buttons_value) )
            self.mif_file.write('\t\t// R19 <- ')
            self.mif_file.write('{0:016b}'.format(buttons_value & 0b1111111111111111) + ' (Value for ' + action_labels_list[index] + ')\n')
 
            self.mif_file.write('\tAND R18, R8')
            self.mif_file.write('\t\t\t\t// Check if ' + action_labels_list[index] + ' are pressed\n')
 
            self.mif_file.write('\tCMP R18, R19\n')
 
            self.mif_file.write('\tBRFL ' + '{:>10}'.format(action_labels_list[index]) + ', 4, 1')
            self.mif_file.write('\t// Branch to ' + action_labels_list[index] + ' if RFLAGS[4] = 1 (EQUAL=1)\n')
 
            self.mif_file.write(return_labels_list[index] + ':\n')
            self.mif_file.write('\n')
 
        self.mif_file.write('\tIRET\n\n')
 
        # Take actions and jump back
 
        # Actions for UP button
        for index in range(0, 8):
            self.mif_file.write('\t// ACTIONS FOR ' + action_labels_list[index*4] + ' BUTTONS PRESSED\n')
            self.mif_file.write(action_labels_list[index*4] + ':\n')
 
            self.mif_file.write('\tLIMM R9, ' + '{:<6}'.format(start_row-15) )
            self.mif_file.write('\t\t\t\t// R9 <- ')
            self.mif_file.write(str(start_row-15) + ' (Start row value - 15 = ' + str(start_row) + ' - 15)\n')
 
            self.mif_file.write('\tCMP R1' + str(index) + ', R9')
            self.mif_file.write('\t\t\t\t\t// R1' + str(index) + ' (Sprite Level ' + str(index) + ' Row register)\n')
 
            self.mif_file.write('\tBRFL ' + '{:>10}_RST'.format(action_labels_list[index*4]) + ', 4, 1')
            self.mif_file.write('\t// Branch to ' + action_labels_list[index*4] + '_RST if RFLAGS[4] = 1 (EQUAL=1)\n')
 
            self.mif_file.write('\tBRFL ' + '{:>10}_RST'.format(action_labels_list[index*4]) + ', 3, 1')
            self.mif_file.write('\t// Branch to ' + action_labels_list[index*4] + '_RST if RFLAGS[3] = 1 (BELOW=1)\n')
 
            self.mif_file.write('\n')
 
            self.mif_file.write('\tLIMM R9, ' + '{:<6}'.format(1) )
            self.mif_file.write('\t\t\t\t// R9 <- 1\n')
 
            self.mif_file.write('\tSUB R1' + str(index) + ', R' + '{:<7}'.format(9) )
            self.mif_file.write('\t\t\t// R1' + str(index) + ' <- R1' + str(index) + ' - R9 (Decrements row)\n' )
 
            self.mif_file.write('\tLIMM R9, ' + '{:<6}'.format(index) )
            self.mif_file.write('\t\t\t\t// R9 <- ' + str(index) + ' (SPRITE_LEVEL = ' + str(index) + ')\n')
 
            self.mif_file.write('\tSPRITE_POS R9, R1' + str(index) + ', R2' + str(index))
            self.mif_file.write('\t\t// SPRITE_POS LEVEL, ROW, COLUMN\n')
 
            self.mif_file.write('\tJMP ' + return_labels_list[index*4] + '\n')
 
            self.mif_file.write('\n')
 
            self.mif_file.write(action_labels_list[index*4] + '_RST:\n')
 
            self.mif_file.write('\tLIMM R1' + str(index) + ', {:<6}'.format(end_row) )
            self.mif_file.write('\t\t\t// R1' + str(index) + ' <- ')
            self.mif_file.write(str(end_row) + ' (End row value = ' + str(end_row) + ')\n')
 
            self.mif_file.write('\tLIMM R9, ' + '{:<6}'.format(index) )
            self.mif_file.write('\t\t\t\t// R9 <- ' + str(index) + ' (SPRITE_LEVEL = ' + str(index) + ')\n')
 
            self.mif_file.write('\tSPRITE_POS R9, R1' + str(index) + ', R2' + str(index))
            self.mif_file.write('\t\t// SPRITE_POS LEVEL, ROW, COLUMN\n')
 
            self.mif_file.write('\tJMP ' + return_labels_list[index*4] + '\n')
            self.mif_file.write('\n\n\n')
 
 
        # Actions for DOWN button
        for index in range(0, 8):
            self.mif_file.write('\t// ACTIONS FOR ' + action_labels_list[(index*4)+1] + ' BUTTONS PRESSED\n')
            self.mif_file.write(action_labels_list[(index*4)+1] + ':\n')
 
            self.mif_file.write('\tLIMM R9, ' + '{:<6}'.format(end_row) )
            self.mif_file.write('\t\t\t\t// R9 <- ')
            self.mif_file.write(str(end_row) + ' (End row value = ' + str(end_row) + ')\n')
 
            self.mif_file.write('\tCMP R1' + str(index) + ', R9')
            self.mif_file.write('\t\t\t\t\t// R1' + str(index) + ' (Sprite Level ' + str(index) + ' Row register)\n')
 
            self.mif_file.write('\tBRFL ' + '{:>10}_RST'.format(action_labels_list[(index*4)+1]) + ', 4, 1')
            self.mif_file.write('\t// Branch to ' + action_labels_list[(index*4)+1] + '_RST if RFLAGS[4] = 1 (EQUAL=1)\n')
 
            self.mif_file.write('\tBRFL ' + '{:>10}_RST'.format(action_labels_list[(index*4)+1]) + ', 5, 1')
            self.mif_file.write('\t// Branch to ' + action_labels_list[(index*4)+1] + '_RST if RFLAGS[5] = 1 (ABOVE=1)\n')
 
            self.mif_file.write('\n')
 
            self.mif_file.write('\tLIMM R9, ' + '{:<6}'.format(1) )
            self.mif_file.write('\t\t\t\t// R9 <- 1\n')
 
            self.mif_file.write('\tADD R1' + str(index) + ', R' + '{:<7}'.format(9) )
            self.mif_file.write('\t\t\t// R1' + str(index) + ' <- R1' + str(index) + ' + R9 (Increments row)\n' )
 
            self.mif_file.write('\tLIMM R9, ' + '{:<6}'.format(index) )
            self.mif_file.write('\t\t\t\t// R9 <- ' + str(index) + ' (SPRITE_LEVEL = ' + str(index) + ')\n')
 
            self.mif_file.write('\tSPRITE_POS R9, R1' + str(index) + ', R2' + str(index))
            self.mif_file.write('\t\t// SPRITE_POS LEVEL, ROW, COLUMN\n')
 
            self.mif_file.write('\tJMP ' + return_labels_list[(index*4)+1] + '\n')
 
            self.mif_file.write('\n')
 
            self.mif_file.write(action_labels_list[(index*4)+1] + '_RST:\n')
 
            self.mif_file.write('\tLIMM R1' + str(index) + ', {:<6}'.format(start_row-15) )
            self.mif_file.write('\t\t\t// R1' + str(index) + ' <- ')
            self.mif_file.write(str(start_row-15) + ' (Start row value - 15 = ' + str(start_row) + ' - 15)\n')
 
            self.mif_file.write('\tLIMM R9, ' + '{:<6}'.format(index) )
            self.mif_file.write('\t\t\t\t// R9 <- ' + str(index) + ' (SPRITE_LEVEL = ' + str(index) + ')\n')
 
            self.mif_file.write('\tSPRITE_POS R9, R1' + str(index) + ', R2' + str(index))
            self.mif_file.write('\t\t// SPRITE_POS LEVEL, ROW, COLUMN\n')
 
            self.mif_file.write('\tJMP ' + return_labels_list[(index*4)+1] + '\n')
            self.mif_file.write('\n\n\n')
 
 
        # Actions for LEFT button
        for index in range(0, 8):
            self.mif_file.write('\t// ACTIONS FOR ' + action_labels_list[(index*4)+2] + ' BUTTONS PRESSED\n')
            self.mif_file.write(action_labels_list[(index*4)+2] + ':\n')
 
            self.mif_file.write('\tLIMM R9, ' + '{:<6}'.format(start_col-15) )
            self.mif_file.write('\t\t\t\t// R9 <- ')
            self.mif_file.write(str(start_col-15) + ' (Start column value - 15 = ' + str(start_col) + ' - 15)\n')
 
            self.mif_file.write('\tCMP R2' + str(index) + ', R9')
            self.mif_file.write('\t\t\t\t\t// R2' + str(index) + ' (Sprite Level ' + str(index) + ' Column register)\n')
 
            self.mif_file.write('\tBRFL ' + '{:>10}_RST'.format(action_labels_list[(index*4)+2]) + ', 4, 1')
            self.mif_file.write('\t// Branch to ' + action_labels_list[(index*4)+2] + '_RST if RFLAGS[4] = 1 (EQUAL=1)\n')
 
            self.mif_file.write('\tBRFL ' + '{:>10}_RST'.format(action_labels_list[(index*4)+2]) + ', 3, 1')
            self.mif_file.write('\t// Branch to ' + action_labels_list[(index*4)+2] + '_RST if RFLAGS[3] = 1 (BELOW=1)\n')
 
            self.mif_file.write('\n')
 
            self.mif_file.write('\tLIMM R9, ' + '{:<6}'.format(1) )
            self.mif_file.write('\t\t\t\t// R9 <- 1\n')
 
            self.mif_file.write('\tSUB R2' + str(index) + ', R' + '{:<7}'.format(9) )
            self.mif_file.write('\t\t\t// R2' + str(index) + ' <- R2' + str(index) + ' - R9 (Decrements column)\n' )
 
            self.mif_file.write('\tLIMM R9, ' + '{:<6}'.format(index) )
            self.mif_file.write('\t\t\t\t// R9 <- ' + str(index) + ' (SPRITE_LEVEL = ' + str(index) + ')\n')
 
            self.mif_file.write('\tSPRITE_POS R9, R1' + str(index) + ', R2' + str(index))
            self.mif_file.write('\t\t// SPRITE_POS LEVEL, ROW, COLUMN\n')
 
            self.mif_file.write('\tJMP ' + return_labels_list[(index*4)+2] + '\n')
 
            self.mif_file.write('\n')
 
            self.mif_file.write(action_labels_list[(index*4)+2] + '_RST:\n')
 
            self.mif_file.write('\tLIMM R2' + str(index) + ', {:<6}'.format(end_col) )
            self.mif_file.write('\t\t\t// R2' + str(index) + ' <- ')
 
            self.mif_file.write(str(end_col) + ' (End column value = ' + str(end_col) + ')\n')
 
 
            self.mif_file.write('\tLIMM R9, ' + '{:<6}'.format(index) )
            self.mif_file.write('\t\t\t\t// R9 <- ' + str(index) + ' (SPRITE_LEVEL = ' + str(index) + ')\n')
 
            self.mif_file.write('\tSPRITE_POS R9, R1' + str(index) + ', R2' + str(index))
            self.mif_file.write('\t\t// SPRITE_POS LEVEL, ROW, COLUMN\n')
 
            self.mif_file.write('\tJMP ' + return_labels_list[(index*4)+2] + '\n')
            self.mif_file.write('\n\n\n')
 
 
        # Actions for RIGHT button
        for index in range(0, 8):
            self.mif_file.write('\t// ACTIONS FOR ' + action_labels_list[(index*4)+3] + ' BUTTONS PRESSED\n')
            self.mif_file.write(action_labels_list[(index*4)+3] + ':\n')
 
            self.mif_file.write('\tLIMM R9, ' + '{:<6}'.format(end_col) )
            self.mif_file.write('\t\t\t\t// R9 <- ')
            self.mif_file.write(str(end_col) + ' (End column value = ' + str(end_col) + ')\n')
 
            self.mif_file.write('\tCMP R2' + str(index) + ', R9')
            self.mif_file.write('\t\t\t\t\t// R2' + str(index) + ' (Sprite Level ' + str(index) + ' Column register)\n')
 
            self.mif_file.write('\tBRFL ' + '{:>10}_RST'.format(action_labels_list[(index*4)+3]) + ', 4, 1')
            self.mif_file.write('\t// Branch to ' + action_labels_list[(index*4)+3] + '_RST if RFLAGS[4] = 1 (EQUAL=1)\n')
 
            self.mif_file.write('\tBRFL ' + '{:>10}_RST'.format(action_labels_list[(index*4)+3]) + ', 5, 1')
            self.mif_file.write('\t// Branch to ' + action_labels_list[(index*4)+3] + '_RST if RFLAGS[5] = 1 (ABOVE=1)\n')
 
            self.mif_file.write('\n')
 
            self.mif_file.write('\tLIMM R9, ' + '{:<6}'.format(1) )
            self.mif_file.write('\t\t\t\t// R9 <- 1\n')
 
            self.mif_file.write('\tADD R2' + str(index) + ', R' + '{:<7}'.format(9) )
            self.mif_file.write('\t\t\t// R2' + str(index) + ' <- R2' + str(index) + ' + R9 (Increments column)\n' )
 
            self.mif_file.write('\tLIMM R9, ' + '{:<6}'.format(index) )
            self.mif_file.write('\t\t\t\t// R9 <- ' + str(index) + ' (SPRITE_LEVEL = ' + str(index) + ')\n')
 
            self.mif_file.write('\tSPRITE_POS R9, R1' + str(index) + ', R2' + str(index))
            self.mif_file.write('\t\t// SPRITE_POS LEVEL, ROW, COLUMN\n')
 
            self.mif_file.write('\tJMP ' + return_labels_list[(index*4)+3] + '\n')
 
            self.mif_file.write('\n')
 
            self.mif_file.write(action_labels_list[(index*4)+3] + '_RST:\n')
 
            self.mif_file.write('\tLIMM R2' + str(index) + ', {:<6}'.format(start_col-15) )
            self.mif_file.write('\t\t\t// R2' + str(index) + ' <- ')
            self.mif_file.write(str(start_col-15) + ' (Start column value - 15 = ' + str(start_col) + ' - 15)\n')
 
 
            self.mif_file.write('\tLIMM R9, ' + '{:<6}'.format(index) )
            self.mif_file.write('\t\t\t\t// R9 <- ' + str(index) + ' (SPRITE_LEVEL = ' + str(index) + ')\n')
 
            self.mif_file.write('\tSPRITE_POS R9, R1' + str(index) + ', R2' + str(index))
            self.mif_file.write('\t\t// SPRITE_POS LEVEL, ROW, COLUMN\n')
 
            self.mif_file.write('\tJMP ' + return_labels_list[(index*4)+3] + '\n')
            self.mif_file.write('\n\n\n')
 
 
        self.mif_file.close()
 
        messagebox.showinfo(parent=self.root, title='Info', message='Done!')
 
        self.quit()
 
 
 
if __name__ == '__main__':
 
    gui = GUI()
 
 

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.