Package: puc8
PUC8Assembler and C compiler for the PUC8 processorCopyright 2020-2023 Wouter CaarlsThis program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.You should have received a copy of the GNU General Public License
along with this program.  If not, seehttp://www.gnu.org/licenses/.IntroductionPUC8 is a microcontroller with 8-bit registers. It is used in the
ENG1448 course of PUC-Rio. This is the assembler and C compiler
infrastructure for it.Instruction set architectureThis very simple processor is a Harvard design, with 17-bit instructions and 8-bit data values. Both instruction and data memories have 256 addresses.RegistersThere are 16 registers.r14issp;r15ispc. The C compiler usesr13asfp.InstructionsAll ALU instructions and MOV set flags.Group(2)Op(2)Imm(1)Nibble 1(4)Nibble 2(4)Nibble 3(4)MnmEffectExample00000rdrsc4iLDRrd <- [rs + c4i]ldr  r0, [r1, 4]00001rdc8u(7..4)c8u(3..0)LDRrd <- [c8u]ldr  r0, [254]00010rs1rs2c4iSTR[rs2 + c4i] <- rs1str  r0, [r1, 4]00011rsc8u(7..4)c8u(3..0)STR[c8u] <- rsstr  r0, [254]00100rdrs0000MOVrd <- rsmov  r0, r100101rdc8u(7..4)c8u(3..0)MOVrd <- c8umov  r0, 25400110condrsc4iBif cond then pc <- rs + c4ib    pc, -400111condc8u(7..4)c8u(3..0)Bif cond then pc <- c8ub    25401000rs00000000PUSH[sp] <- rs, sp <- sp - 1push r0010100000rs0000CALL[sp] <- pc + 1, sp <- sp - 1, pc <- rscall r0010110000c8u(7..4)c8u(3..0)CALL[sp] <- pc + 1, sp <- sp - 1, pc <- c8ucall 25401100rd00000000POPrd <- [sp + 1], sp <- sp + 1pop  r010000rdrs1rs2ADDrd <- rs1 + rs2add  r0, r1, r210001rdrsc4uADDrd <- rs + c4uadd  r0, r1, 410010rdrs1rs2SUBrd <- rs1 - rs2sub  r0, r1, r210011rdrsc4uSUBrd <- rs - c4usub  r0, r1, 410100rdrs1rs2SHLrd <- rs1 << rs2shl  r0, r1, r2[^1]10101rdrsc4uSHLrd <- rs << c4ushl  r0, r1, 1[^1]10110rdrs1rs2SHRrd <- rs1 >> rs2shr  r0, r1, r2[^1]10111rdrsc4uSHRrd <- rs >> c4ushr  r0, r1, 1[^1]11000rdrs1rs2ANDrd <- rs1 & rs2and  r0, r1, r211001rdrsc4uANDrd <- rs & (1<<c4u)and  r0, r1, 411010rdrs1rs2ORRrd <- rs1 | rs2orr  r0, r1, r211011rdrsc4uORRrd <- rs | (1<<c4u)orr  r0, r1, 411100rdrs1rs2EORrd <- rs1 ^ rs2eor  r0, r1, r211101rdrsc4uEORrd <- rs ^ (1<<c4u)eor  r0, r1, 4[^1]: May be implemented as a constant shift of 1.c4ican be omitted from the assembly instruction, in which case it is set
to 0.Pseudo-instructionsPseudo-instructionActual instructionbeqbzbnebnzbhsbcsblobccretpop pcCondition codesConditionMeaning0000Unconditional0001Zero flag set0010Zero flag not set0011Carry flag set0100Carry flag not set0101Signed less than0110Signed greater than or equalAssembly languageAssembly statements generally follow the following structure[LABEL:]MNEMONIC[OPERAND[,OPERAND]...]The availableMNEMONICs can be found in the table above.OPERANDs can be registers, constants, or labels. Labels used as operands must be prefixed with@:loop:jmp@loopWhen the operand is used as the contents of a memory address, it must be enclosed in square brackets:ldrr0,[@inp].sectiondatainp:.db0Apart from these statements, the assembler recognizes the following directives:.include"FILE"Includes a givenFILE. The path is relative to the file being processed..sectionSECTIONDefine into which memorySECTIONthe subsequent code is assembled. Options arecodeanddata. The default iscode..orgADDRESSSets memoryADDRESSat which the subsequent code will be assembled..equLABELVALUECreates aLABELfor a specific constantVALUE. Values may be character constants, e.g."c".dbVALUEInserts aVALUEinto the instruction stream. The value may be a string constant, e.g."Hello, world".macroNAME; code.endmacroDefines a macro. The code inside the macro can use arguments of the form$0,$1, etc., which are replaced by the actual arguments when the macro is called usingNAME arg0, arg1. Labels inside the macro that start with an underscore are localized such that the same macro can be called multiple times.Installationpip install puc8orgit clone https://github.com/wcaarls/puc8
cd puc8
pip install .Usageusage: as-puc8 [-h] [-o OUTPUT] [-s] [-t N] [-E] file

PUC8 Assembler (c) 2020-2023 Wouter Caarls, PUC-Rio

positional arguments:
  file                  ASM source file

options:
  -h, --help            show this help message and exit
  -o OUTPUT, --output OUTPUT
                        Output file
  -s, --simulate        Simulate resulting program
  -t N, --test N        Simulate for 1000 steps and check whether PC == N
  -E                    Output preprocessed assembly codeusage: cc-puc8 [-h] [-o OUTPUT] [-s] [-t N] [-S] [-O {0,1,2}] file

PUC8 C compiler (c) 2020-2023 Wouter Caarls, PUC-Rio

positional arguments:
  file                  C source file

options:
  -h, --help            show this help message and exit
  -o OUTPUT, --output OUTPUT
                        Output file
  -s, --simulate        Simulate resulting program
  -t N, --test N        Simulate for 1000 steps and check whether PC == N
  -S                    Output assembly code
  -O {0,1,2}            Optimization levelExamplesDirectly compile C to VHDL./cc-puc8 examples/c/hello.cCreate assembly from C./cc-puc8 examples/c/hello.c -SAssemble to VHDL code./as-puc8 examples/asm/ps2_lcd.asmAssemble to VHDL package./as-puc8 examples/asm/ps2_lcd.asm -o ps2_lcd.vhdlSimulate resulting C or assembly program./cc-puc8 -O0 examples/c/unittest.c -s
./as-puc8 examples/asm/simple.asm -sAcknowledgmentsThe C compiler is based onPPCI.Copyright (c) 2011-2019, Windel BouwmanAll rights reserved.Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.