micro_asm_test.py revision 4502
14483Sgblack@eecs.umich.edu# Copyright (c) 2007 The Regents of The University of Michigan 24483Sgblack@eecs.umich.edu# All rights reserved. 34483Sgblack@eecs.umich.edu# 44483Sgblack@eecs.umich.edu# Redistribution and use in source and binary forms, with or without 54483Sgblack@eecs.umich.edu# modification, are permitted provided that the following conditions are 64483Sgblack@eecs.umich.edu# met: redistributions of source code must retain the above copyright 74483Sgblack@eecs.umich.edu# notice, this list of conditions and the following disclaimer; 84483Sgblack@eecs.umich.edu# redistributions in binary form must reproduce the above copyright 94483Sgblack@eecs.umich.edu# notice, this list of conditions and the following disclaimer in the 104483Sgblack@eecs.umich.edu# documentation and/or other materials provided with the distribution; 114483Sgblack@eecs.umich.edu# neither the name of the copyright holders nor the names of its 124483Sgblack@eecs.umich.edu# contributors may be used to endorse or promote products derived from 134483Sgblack@eecs.umich.edu# this software without specific prior written permission. 144483Sgblack@eecs.umich.edu# 154483Sgblack@eecs.umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 164483Sgblack@eecs.umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 174483Sgblack@eecs.umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 184483Sgblack@eecs.umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 194483Sgblack@eecs.umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 204483Sgblack@eecs.umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 214483Sgblack@eecs.umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 224483Sgblack@eecs.umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 234483Sgblack@eecs.umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 244483Sgblack@eecs.umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 254483Sgblack@eecs.umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 264483Sgblack@eecs.umich.edu# 274483Sgblack@eecs.umich.edu# Authors: Gabe Black 284483Sgblack@eecs.umich.edu 294483Sgblack@eecs.umich.edufrom micro_asm import MicroAssembler, Macroop, Rom 304483Sgblack@eecs.umich.edu 314483Sgblack@eecs.umich.educlass Bah(object): 324483Sgblack@eecs.umich.edu def __init__(self): 334483Sgblack@eecs.umich.edu self.mnemonic = "bah" 344483Sgblack@eecs.umich.edu 354483Sgblack@eecs.umich.educlass Bah_Tweaked(object): 364483Sgblack@eecs.umich.edu def __init__(self): 374483Sgblack@eecs.umich.edu self.mnemonic = "bah_tweaked" 384483Sgblack@eecs.umich.edu 394483Sgblack@eecs.umich.educlass Hoop(object): 404483Sgblack@eecs.umich.edu def __init__(self, first_param, second_param): 414483Sgblack@eecs.umich.edu self.mnemonic = "hoop_%s_%s" % (first_param, second_param) 424483Sgblack@eecs.umich.edu def __str__(self): 434483Sgblack@eecs.umich.edu return "%s" % self.mnemonic 444483Sgblack@eecs.umich.edu 454483Sgblack@eecs.umich.educlass Dah(object): 464483Sgblack@eecs.umich.edu def __init__(self): 474483Sgblack@eecs.umich.edu self.mnemonic = "dah" 484483Sgblack@eecs.umich.edu 494483Sgblack@eecs.umich.edumicroops = { 504483Sgblack@eecs.umich.edu "bah": Bah, 514483Sgblack@eecs.umich.edu "hoop": Hoop, 524483Sgblack@eecs.umich.edu "dah": Dah 534483Sgblack@eecs.umich.edu} 544483Sgblack@eecs.umich.edu 554483Sgblack@eecs.umich.educlass TestMacroop(Macroop): 564483Sgblack@eecs.umich.edu def tweak(self): 574483Sgblack@eecs.umich.edu microops["bah"] = Bah_Tweaked 584483Sgblack@eecs.umich.edu def untweak(self): 594483Sgblack@eecs.umich.edu microops["bah"] = Bah 604483Sgblack@eecs.umich.edu 614483Sgblack@eecs.umich.edu def __init__(self, name): 624483Sgblack@eecs.umich.edu super(TestMacroop, self).__init__(name) 634483Sgblack@eecs.umich.edu self.directives = { 644483Sgblack@eecs.umich.edu "tweak": self.tweak, 654483Sgblack@eecs.umich.edu "untweak": self.untweak 664483Sgblack@eecs.umich.edu } 674483Sgblack@eecs.umich.edu 684483Sgblack@eecs.umich.eduassembler = MicroAssembler(TestMacroop, microops, Rom('main ROM')) 694483Sgblack@eecs.umich.edu 704483Sgblack@eecs.umich.edutestAssembly = ''' 714502Sgblack@eecs.umich.edu# Single line comment 724502Sgblack@eecs.umich.edu 734483Sgblack@eecs.umich.edudef rom { 744483Sgblack@eecs.umich.edu goo: bah 754483Sgblack@eecs.umich.edu extern la: hoop 4*8, "a" 764502Sgblack@eecs.umich.edu}; /* multiline comment on one line */ 774502Sgblack@eecs.umich.edu 784502Sgblack@eecs.umich.edu/* multi line comment across lines 794502Sgblack@eecs.umich.edu to make sure they work */ 804483Sgblack@eecs.umich.edu 814483Sgblack@eecs.umich.edudef macroop squishy { 824483Sgblack@eecs.umich.edu .tweak 834483Sgblack@eecs.umich.edu bah 844483Sgblack@eecs.umich.edu .untweak 854483Sgblack@eecs.umich.edu bah 864502Sgblack@eecs.umich.edu dah # single line comment after something 874483Sgblack@eecs.umich.edu .tweak 884483Sgblack@eecs.umich.edu}; 894483Sgblack@eecs.umich.edu 904483Sgblack@eecs.umich.edudef macroop squashy { 914483Sgblack@eecs.umich.edu bah 924483Sgblack@eecs.umich.edu}; 934483Sgblack@eecs.umich.edu 944502Sgblack@eecs.umich.edudef macroop jumper (bar); 954483Sgblack@eecs.umich.edu''' 964483Sgblack@eecs.umich.eduassembler.assemble(testAssembly) 97