canardconfit/cc-compiler

By canardconfit

Updated about 2 years ago

A custom compiler project for HEPIA students to compile C-like for their own processor.

Image
Languages & frameworks
Developer tools
Web servers
0

10K+

canardconfit/cc-compiler repository overview

CC Processor Compiler

Docker Pulls GitHub Release GitHub Repo stars

Description

The cc-compiler project is developed as part of an exercise at HEPIA in Geneva, where students are tasked with creating their own processor using Logisim. This project aims to provide a Node.js-based compiler that can translate high-level language instructions into machine code compatible with the custom processor designed in Logisim.

C-Like Syntax

SyntaxArgumentsExampleDescription
R[0-7] = xx = Any int number (8 bits)R0 = 0Assignation
R[0-7] = R[0-7] + R[0-7]R0 = R1 + R2Addition
R[0-7] = R[0-7] - R[0-7]R0 = R1 - R2Substraction
R[0-7] = R[0-7] >> 1R0 = R1 >> 1Shift right by 1
R[0-7] = R[0-7] << 1R0 = R1 << 1Shift left by 1
R[0-7] = R[0-7] ASR R[0-7]R0 = R1 ASR R2ASR shift
R[0-7] = R[0-7] and R[0-7]R0 = R1 and R2And operation
R[0-7] = R[0-7] or R[0-7]R0 = R1 or R2Or operation
R[0-7] = not R[0-7]R0 = not R1Not operation
while x yx = (not)
y = N,Z,C,V, True
while TrueWhile
if R[0-7] x R[0-7]x = ==, !=, <=, >=, <, >if R0 == R1If
{`Brace to start if or while block
}`Brace to end if or while block
STORE R[0-7] R[0-7] xx = offset intSTORE R0 R1 0Store from value into R[0-7] to RAM or peripheral (R[0-7] value pointer + offset)
LOAD R[0-7] R[0-7] xx = offset intLOAD R1 R2 0Store from RAM or peripheral (R[0-7] value pointer + offset) to R[0-7] variable
JMP xx = Any int number (signed)JMP 2Jump unconditional relative.

Assembler instructions

ALU instructions
OperationOpcodeResultSource 0Source 1Reserved
Bits15 14 13 1211 10 0908 07 0605 04 0302 01 00
RD = RS0 + RS10000RDRS0RS1
RD = RS0 - RS10001RDRS0RS1
RD = RS0 << 10010RDRS0
RD = RS0 >> 10011RDRS0
RD = ASR(RS0)0100RDRS0
RD = RS0 AND RS10101RDRS0RS1
RD = RS0 OR RS10110RDRS0RS1
RD = NOT (RS0)0111RDRS0
Initialization with constant instructions
OperationOpcodeResultReservedConstant
Bits15 14 13 1211 10 090807 06 05 04 03 02 01 00
RD = val1000RD-val
Unconditional jump instructions
OperationOpcodeReservedNumber of jump (signed)
Bits15 14 13 1211 10 09 0807 06 05 04 03 02 01 00
B : PC = PC + val1011-val
Conditional jump instructions
OperationOpcodeConditionNumber of jump (signed)
Bits15 14 13 1211 10 09 0807 06 05 04 03 02 01 00
BC: If(cond), PC = PC + val1010CONDval

COND = Z,N,C,V.

Branch instructions (TODO)
OperationOpcodeReg LinkReservedAddress to the function
Bits15 14 13 1211 10 09 080807 06 05 04 03 02 01 00
BL : PC = val ; RL = PC + 11110RL-val
BR : PC = [RL]1111RL--
Memory access instructions (TODO)
InstructionOpcodeResultPointeroffset
Bits15 14 13 1211 10 0908 07 0605 04 03 02 01 00
LD RD, offset[RP]1100RDRPoffset
ST RS, offset[RP]1101RSRPoffset
Peripheral access table
DescriptionPointer valueAccessPointeroffset
Leds128R / W1280
Bit 0 - LED0
Bit 1 - LED1
Bit 2 - LED2
Bit 3 - LED3
Bit 4 - LED4
Bit 5 - LED5
Bit 6 - LED6
Bit 7 - LED7
Buttons129R1281
Bit 0 - BTN0
Bit 1 - BTN1
Bit 2 - BTN2
Bit 3 - BTN3
Bit 4 - BTN4
Bit 5 - BTN5
Bit 6 - BTN6
Bit 7 - BTN7
UART
Registry Address130R / W1282
Bit 0 - Addr bit 0
Bit 1 - Addr bit 1
Bit 2 - Addr bit 2
Bit 3 - Addr bit 3
Bit 4 - Reserved
Bit 5 - Reserved
Bit 6 - Reserved
Bit 7 - Reserved
DataL131R1283
Bit 0
Bit 1
Bit 2
Bit 3
Bit 4
Bit 5
Bit 6
Bit 7
DataH132R1284
Bit 8
Bit 9
Bit 10
Bit 11
Bit 12
Bit 13
Bit 14
Bit 15
PWM
V1133W1285
V2134W1286
Other
Valve135R / W1287
Bit 0 - Valve bit 0
Bit 1 - Reserved
Bit 2 - Reserved
Bit 3 - Reserved
Bit 4 - Reserved
Bit 5 - Reserved
Bit 6 - Reserved
Bit 7 - Reserved
SPI1
DataL136R1288
Bit 0
Bit 1
Bit 2
Bit 3
Bit 4
Bit 5
Bit 6
Bit 7
DataH137R1289
Bit 8
Bit 9
Bit 10
Bit 11
Bit 12
Bit 13
Bit 14
Bit 15
UART Registries Table
AddressValueNameDescription
00000x0VersionFirmware version of the RobotMyLab
00010x1Right DistDistance measured by the front right sensor
00100x2Front DistDistance measured by the front sensor
00110x3Left DistDistance measured by the front left sensor
01000x4Accel XX-axis of the accelerometer (front-back axis)
01010x5Accel YY-axis of the accelerometer (left-right axis)
01100x6Accel ZZ-axis of the accelerometer (up-down axis)
01110x7Gyro XAngular velocity around the X-axis
10000x8Gyro YAngular velocity around the Y-axis
10010x9Gyro ZAngular velocity around the Z-axis
10100xABatteryCharge state of the rechargeable batteries
10110xBLeft IRValue measured by the left ground IR sensor
11000xCRight IRValue measured by the right ground IR sensor
11010xDLeft OdomCumulative distance measured by the left odometric sensor
11100xERight OdomCumulative distance measured by the right odometric sensor
11110xFIR RXValue measured by the IR communication receiver

Examples

R0 = 0
while true
{
    R1 = 0
    R2 = 1
    while not N
    {
        R3 = R1 + R0
        R1 = R2 + R0
        R2 = R2 + R3
    }
}

Compiled to:

0 0x8000
1 0x8200
2 0x8401
3 0x0640
4 0x0280
5 0x0498
6 0xA402
7 0xB0FC
8 0xB0F9

Another example:

// -----
// Test whether the value of a peripheral variable is 30
// -----
R1 = 127

// Load data from R1 pointer with offset 5 into R2 var
LOAD R2 R1 5

// Test variable
R3 = 30

if R2 == R3
{
    R4 = 1
}
// Else
if R2 != R3
{
    R4 = 0
}

// Reassign pointer to store
R1 = 0

// Store result into RAM at 0x0
STORE R4 R1 0

Compiled to:

0x827F
0xC445
0x861E
0x2D0
0xA802
0xB002
0x8801
0x2D0
0xA803
0xB001
0x8800
0x8200
0xD840

Another example:

// -----
// Get Value from UART
// -----

// Pointer to Perihperal part
R1 = 128

// Address of the wanted UART registry
R2 = 2

// Store address wanted into UART address peripheral
STORE R2 R1 2

while true
{
    // Load data incoming from UART registry selected
    LOAD R0 R1 3

    // Display value from UART to Leds
    STORE R0 R1 0
}

Compiled to:

0x8280
0x8402
0xD442
0xC043
0xD040
0xB0FE

Tag summary

Content type

Image

Digest

sha256:f3e3158c4

Size

964.8 MB

Last updated

about 2 years ago

docker pull canardconfit/cc-compiler