micro_asm_test.py revision 4507
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
294507Sgblack@eecs.umich.edufrom micro_asm import MicroAssembler, Combinational_Macroop, Rom_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
554507Sgblack@eecs.umich.educlass TestMacroop(Combinational_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
604503Sgblack@eecs.umich.edu    def print_debug(self, message):
614503Sgblack@eecs.umich.edu        print message
624483Sgblack@eecs.umich.edu
634483Sgblack@eecs.umich.edu    def __init__(self, name):
644483Sgblack@eecs.umich.edu        super(TestMacroop, self).__init__(name)
654483Sgblack@eecs.umich.edu        self.directives = {
664483Sgblack@eecs.umich.edu            "tweak": self.tweak,
674503Sgblack@eecs.umich.edu            "untweak": self.untweak,
684503Sgblack@eecs.umich.edu            "print": self.print_debug
694483Sgblack@eecs.umich.edu        }
704483Sgblack@eecs.umich.edu
714507Sgblack@eecs.umich.eduassembler = MicroAssembler(TestMacroop, microops, Rom('main ROM'), Rom_Macroop)
724483Sgblack@eecs.umich.edu
734483Sgblack@eecs.umich.edutestAssembly = '''
744502Sgblack@eecs.umich.edu# Single line comment
754502Sgblack@eecs.umich.edu
764483Sgblack@eecs.umich.edudef rom {
774483Sgblack@eecs.umich.edu    goo: bah
784483Sgblack@eecs.umich.edu    extern la: hoop 4*8, "a"
794502Sgblack@eecs.umich.edu}; /* multiline comment on one line */
804502Sgblack@eecs.umich.edu
814502Sgblack@eecs.umich.edu/* multi line comment across lines
824502Sgblack@eecs.umich.edu   to make sure they work */
834483Sgblack@eecs.umich.edu
844483Sgblack@eecs.umich.edudef macroop squishy {
854483Sgblack@eecs.umich.edu    .tweak
864483Sgblack@eecs.umich.edu    bah
874483Sgblack@eecs.umich.edu    .untweak
884503Sgblack@eecs.umich.edu    .print "In the midst"
894483Sgblack@eecs.umich.edu    bah
904502Sgblack@eecs.umich.edu    dah # single line comment after something
914483Sgblack@eecs.umich.edu    .tweak
924483Sgblack@eecs.umich.edu};
934483Sgblack@eecs.umich.edu
944483Sgblack@eecs.umich.edudef macroop squashy {
954483Sgblack@eecs.umich.edu    bah
964483Sgblack@eecs.umich.edu};
974483Sgblack@eecs.umich.edu
984502Sgblack@eecs.umich.edudef macroop jumper (bar);
994483Sgblack@eecs.umich.edu'''
1004483Sgblack@eecs.umich.eduassembler.assemble(testAssembly)
101