|
| 1 | +# SPDX-FileCopyrightText: 2021 Jeff Epler, written for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +# pylint: disable=missing-module-docstring,invalid-name,missing-function-docstring,missing-class-docstring |
| 6 | + |
| 7 | +import pathlib |
| 8 | +import sys |
| 9 | +import unittest |
| 10 | + |
| 11 | +sys.path.insert(0, str(pathlib.Path(__file__).absolute().parent.parent)) |
| 12 | + |
| 13 | +import adafruit_pioasm # pylint: disable=wrong-import-position |
| 14 | + |
| 15 | + |
| 16 | +def nice_opcode(o): |
| 17 | + o = f"{o:016b}" |
| 18 | + return o[:3] + "_" + o[3:8] + "_" + o[8:] |
| 19 | + |
| 20 | + |
| 21 | +class TestNop(unittest.TestCase): |
| 22 | + def assertAssemblesTo(self, source, expected): |
| 23 | + actual = adafruit_pioasm.assemble(source) |
| 24 | + expected_bin = [nice_opcode(x) for x in expected] |
| 25 | + actual_bin = [nice_opcode(x) for x in actual] |
| 26 | + self.assertEqual( |
| 27 | + expected_bin, |
| 28 | + actual_bin, |
| 29 | + f"Assembling {source!r}: Expected {expected_bin}, got {actual_bin}", |
| 30 | + ) |
| 31 | + |
| 32 | + def assertAssemblyFails(self, source): |
| 33 | + self.assertRaises(RuntimeError, adafruit_pioasm.assemble, source) |
| 34 | + |
| 35 | + def testNonsense(self): |
| 36 | + self.assertAssemblyFails("nope") |
| 37 | + |
| 38 | + def testNop(self): |
| 39 | + self.assertAssemblesTo("nop", [0b101_00000_010_00_010]) |
| 40 | + self.assertAssemblesTo( |
| 41 | + "nop\nnop", [0b101_00000_010_00_010, 0b101_00000_010_00_010] |
| 42 | + ) |
| 43 | + self.assertAssemblesTo("nop [1]", [0b101_00001_010_00_010]) |
| 44 | + self.assertAssemblesTo(".side_set 1\nnop side 1", [0b101_10000_010_00_010]) |
| 45 | + self.assertAssemblesTo(".side_set 1\nnop side 1 [1]", [0b101_10001_010_00_010]) |
| 46 | + |
| 47 | + def testJmp(self): |
| 48 | + self.assertAssemblesTo("l:\njmp l", [0b000_00000_000_00000]) |
| 49 | + self.assertAssemblesTo("l:\njmp 7", [0b000_00000_000_00111]) |
| 50 | + self.assertAssemblesTo("jmp l\nl:", [0b000_00000_000_00001]) |
| 51 | + self.assertAssemblesTo("jmp !x, l\nl:", [0b000_00000_001_00001]) |
| 52 | + self.assertAssemblesTo("jmp x--, l\nl:", [0b000_00000_010_00001]) |
| 53 | + self.assertAssemblesTo("jmp !y, l\nl:", [0b000_00000_011_00001]) |
| 54 | + self.assertAssemblesTo("jmp y--, l\nl:", [0b000_00000_100_00001]) |
| 55 | + self.assertAssemblesTo("jmp x!=y, l\nl:", [0b000_00000_101_00001]) |
| 56 | + self.assertAssemblesTo("jmp pin, l\nl:", [0b000_00000_110_00001]) |
| 57 | + self.assertAssemblesTo("jmp !osre, l\nl:", [0b000_00000_111_00001]) |
| 58 | + |
| 59 | + def testWait(self): |
| 60 | + self.assertAssemblesTo("wait 0 gpio 0", [0b001_00000_0_00_00000]) |
| 61 | + self.assertAssemblesTo("wait 0 gpio 1", [0b001_00000_0_00_00001]) |
| 62 | + self.assertAssemblesTo("wait 1 gpio 2", [0b001_00000_1_00_00010]) |
| 63 | + self.assertAssemblesTo("wait 0 pin 0", [0b001_00000_0_01_00000]) |
| 64 | + self.assertAssemblesTo("wait 0 pin 1", [0b001_00000_0_01_00001]) |
| 65 | + self.assertAssemblesTo("wait 1 pin 2", [0b001_00000_1_01_00010]) |
| 66 | + self.assertAssemblesTo("wait 0 irq 0", [0b001_00000_0_10_00000]) |
| 67 | + self.assertAssemblesTo("wait 0 irq 0 rel", [0b001_00000_0_10_10000]) |
| 68 | + self.assertAssemblesTo("wait 1 irq 0", [0b001_00000_1_10_00000]) |
| 69 | + self.assertAssemblesTo("wait 0 irq 1 rel", [0b001_00000_0_10_10001]) |
0 commit comments