Initial commit
This commit is contained in:
Binary file not shown.
@@ -0,0 +1,53 @@
|
||||
// *********************************************************************************************
|
||||
// Description : C Program to find prime factors of a Number
|
||||
// Project Version : v1.0
|
||||
// Project : [BCDC] Microtec Academy Course: Building a RISC-V CPU with SystemVerilog
|
||||
// -----
|
||||
// Copyright (c) : 2025 Fraunhofer IIS, Department IDS
|
||||
// Created : 15.Oct.2025 by Hussein Elzomor
|
||||
// Last Modified : 15.Oct.2025 by Hussein Elzomor
|
||||
// -----
|
||||
// HISTORY : Date By Comments
|
||||
// ----------- --------- -------------------------------------------------
|
||||
// *********************************************************************************************
|
||||
|
||||
|
||||
|
||||
asm(".global _start, halt; _start: lui sp 0x2; addi sp sp -4; jal main");
|
||||
|
||||
int main()
|
||||
{
|
||||
int i, iModulo, j, Number, NumberModulo, isPrime, Prime; // Create variables
|
||||
Number = 39; // Set initial value for Number to be factorized
|
||||
Prime = 0; // Set initial value for Prime to be printed
|
||||
for (i = 2; i <= Number; i++) // Iterate over i from 2 to Number, all possible numbers excluding 1 & 0
|
||||
{
|
||||
NumberModulo = Number;
|
||||
while (NumberModulo > 0) // Calculate Number % i using subtraction and store in NumberModulo
|
||||
{
|
||||
NumberModulo -= i;
|
||||
}
|
||||
if (NumberModulo == 0) // If Number % i == 0, it is possible that it's a prime number
|
||||
{
|
||||
isPrime = 1;
|
||||
for (j = 2; j <= i / 2; j++) // Iterate over j from 2 to i/2
|
||||
{
|
||||
iModulo = i;
|
||||
while (iModulo > 0) // Calculate i % j using subtraction and store in iModulo
|
||||
{
|
||||
iModulo -= j;
|
||||
}
|
||||
if (iModulo == 0) // If i % j == 0, it is not a prime number
|
||||
{
|
||||
isPrime = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (isPrime == 1) // Print i if i is a prime number
|
||||
{
|
||||
Prime = i; // Set variable Prime to the prime number. Used for printing in the TB upon change
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0; // End the main function and return 0 on success
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
00002137
|
||||
FFC10113
|
||||
008000EF
|
||||
0000006F
|
||||
FD010113
|
||||
02112623
|
||||
02812423
|
||||
03010413
|
||||
02700793
|
||||
00078813
|
||||
FCF42C23
|
||||
FC042A23
|
||||
00200793
|
||||
FEF42623
|
||||
0C00006F
|
||||
FD842783
|
||||
FEF42023
|
||||
0140006F
|
||||
FE042703
|
||||
FEC42783
|
||||
40F707B3
|
||||
FEF42023
|
||||
FE042783
|
||||
FEF046E3
|
||||
FE042783
|
||||
08079463
|
||||
00100793
|
||||
FCF42E23
|
||||
00200793
|
||||
FEF42223
|
||||
0440006F
|
||||
FEC42783
|
||||
FEF42423
|
||||
0140006F
|
||||
FE842703
|
||||
FE442783
|
||||
40F707B3
|
||||
FEF42423
|
||||
FE842783
|
||||
FEF046E3
|
||||
FE842783
|
||||
00079663
|
||||
FC042E23
|
||||
02C0006F
|
||||
FE442783
|
||||
00178793
|
||||
FEF42223
|
||||
FEC42783
|
||||
01F7D713
|
||||
00F707B3
|
||||
4017D793
|
||||
00078713
|
||||
FE442783
|
||||
FAF754E3
|
||||
FDC42703
|
||||
00100793
|
||||
00F71663
|
||||
FEC42883
|
||||
FD142A23
|
||||
FEC42783
|
||||
00178793
|
||||
FEF42623
|
||||
FEC42703
|
||||
FD842783
|
||||
F2E7DEE3
|
||||
00000793
|
||||
00078513
|
||||
02C12083
|
||||
02812403
|
||||
03010113
|
||||
00008067
|
||||
@@ -0,0 +1,140 @@
|
||||
// *********************************************************************************************
|
||||
// Description : Assembly instructions for prime factors program
|
||||
// Project Version : v1.0
|
||||
// Project : [BCDC] Microtec Academy Course: Building a RISC-V CPU with SystemVerilog
|
||||
// -----
|
||||
// Copyright (c) : 2025 Fraunhofer IIS, Department IDS
|
||||
// Created : 15.Oct.2025 by Hussein Elzomor
|
||||
// Last Modified : 21.Oct.2025 by Hussein Elzomor
|
||||
// -----
|
||||
// HISTORY : Date By Comments
|
||||
// ----------- --------- -------------------------------------------------
|
||||
// *********************************************************************************************
|
||||
|
||||
|
||||
|
||||
# GNU C23 (crosstool-NG UNKNOWN) version 15.2.0 (riscv32-unknown-linux-gnu)
|
||||
# compiled by GNU C version 16.0.0 20250802 (experimental), GMP version 6.3.0, MPFR version 4.2.1, MPC version 1.3.1, isl version isl-0.24-GMP
|
||||
|
||||
# GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
|
||||
# options passed: -mabi=ilp32d -misa-spec=20191213 -mtls-dialect=trad -march=rv32imafdc_zicsr_zifencei_zmmul_zaamo_zalrsc_zca_zcd_zcf -g
|
||||
_start:
|
||||
lui sp, 0x2 # Initialize stack pointer value to size of memory (0x00002000)
|
||||
addi sp, sp -4 # Subtract stack pointer value by 4 to point to the last memory address (0x00001FFC)
|
||||
jal main # Jump to main and save return address to ra
|
||||
halt:
|
||||
j halt # Get stuck here in a loop forever
|
||||
main:
|
||||
addi sp,sp,-48 # Reserve some space on the stack
|
||||
sw ra,44(sp) # Save the return address on the stack
|
||||
sw s0,40(sp) # Save the caller frame pointer value on the stack
|
||||
addi s0,sp,48 # Store the callee frame pointer value in s0 (end address of the stack frame)
|
||||
# primeFactors.c:6: Number = 39; // Set initial value for Number to be factorized
|
||||
li a5,39 # tmp137,39 # Change the Number (39) to different values to test the program
|
||||
mv a6, a5 # ,tmp137 # Used for printing in the TB upon change
|
||||
sw a5,-40(s0) # tmp137, Number
|
||||
# primeFactors.c:7: Prime = 0; // Set initial value for Prime to be printed
|
||||
sw zero,-44(s0) #, Prime
|
||||
loop_i_init:
|
||||
# primeFactors.c:8: for (i = 2; i <= Number; i++) // Iterate over i from 2 to Number, all possible numbers excluding 1 & 0
|
||||
li a5,2 # tmp138,
|
||||
sw a5,-20(s0) # tmp138, i
|
||||
# primeFactors.c:8: for (i = 2; i <= Number; i++) // Iterate over i from 2 to Number, all possible numbers excluding 1 & 0
|
||||
j loop_i_check #
|
||||
num_mod_set:
|
||||
# primeFactors.c:10: NumberModulo = Number;
|
||||
lw a5,-40(s0) # tmp139, Number
|
||||
sw a5,-32(s0) # tmp139, NumberModulo
|
||||
# primeFactors.c:11: while (NumberModulo > 0) // Calculate Number % i using subtraction and store in NumberModulo
|
||||
j num_mod_loop #
|
||||
num_mod_sub_i:
|
||||
# primeFactors.c:13: NumberModulo -= i;
|
||||
lw a4,-32(s0) # tmp141, NumberModulo
|
||||
lw a5,-20(s0) # tmp142, i
|
||||
sub a5,a4,a5 # NumberModulo_21, tmp141, tmp142
|
||||
sw a5,-32(s0) # NumberModulo_21, NumberModulo
|
||||
num_mod_loop:
|
||||
# primeFactors.c:11: while (NumberModulo > 0) // Calculate Number % i using subtraction and store in NumberModulo
|
||||
lw a5,-32(s0) # tmp143, NumberModulo
|
||||
bgt a5,zero,num_mod_sub_i #, tmp143,,
|
||||
num_mod_check:
|
||||
# primeFactors.c:15: if (NumberModulo == 0) // If Number % i == 0, it is possible that it's a prime number
|
||||
lw a5,-32(s0) # tmp144, NumberModulo
|
||||
bne a5,zero,loop_i_inc #, tmp144,,
|
||||
|
||||
# primeFactors.c:17: isPrime = 1;
|
||||
li a5,1 # tmp145,
|
||||
sw a5,-36(s0) # tmp145, isPrime
|
||||
loop_j_init:
|
||||
# primeFactors.c:18: for (j = 2; j <= i / 2; j++) // Iterate over j from 2 to i/2
|
||||
li a5,2 # tmp146,
|
||||
sw a5,-28(s0) # tmp146, j
|
||||
# primeFactors.c:18: for (j = 2; j <= i / 2; j++) // Iterate over j from 2 to i/2
|
||||
j loop_j_check #
|
||||
i_mod_set:
|
||||
# primeFactors.c:20: iModulo = i;
|
||||
lw a5,-20(s0) # tmp147, i
|
||||
sw a5,-24(s0) # tmp147, iModulo
|
||||
# primeFactors.c:21: while (iModulo > 0) // Calculate i % j using subtraction and store in iModulo
|
||||
j i_mod_loop #
|
||||
i_mod_sub_j:
|
||||
# primeFactors.c:23: iModulo -= j;
|
||||
lw a4,-24(s0) # tmp149, iModulo
|
||||
lw a5,-28(s0) # tmp150, j
|
||||
sub a5,a4,a5 # iModulo_18, tmp149, tmp150
|
||||
sw a5,-24(s0) # iModulo_18, iModulo
|
||||
i_mod_loop:
|
||||
# primeFactors.c:21: while (iModulo > 0) // Calculate i % j using subtraction and store in iModulo
|
||||
lw a5,-24(s0) # tmp151, iModulo
|
||||
bgt a5,zero,i_mod_sub_j #, tmp151,,
|
||||
i_mod_check:
|
||||
# primeFactors.c:25: if (iModulo == 0) // If i % j == 0, it is not a prime number
|
||||
lw a5,-24(s0) # tmp152, iModulo
|
||||
bne a5,zero,loop_j_inc #, tmp152,,
|
||||
# primeFactors.c:27: isPrime = 0;
|
||||
sw zero,-36(s0) #, isPrime
|
||||
# primeFactors.c:28: break;
|
||||
j prime_check #
|
||||
loop_j_inc:
|
||||
# primeFactors.c:18: for (j = 2; j <= i / 2; j++) // Iterate over j from 2 to i/2
|
||||
lw a5,-28(s0) # tmp154, j
|
||||
addi a5,a5,1 #, j_16, tmp154
|
||||
sw a5,-28(s0) # j_16, j
|
||||
loop_j_check:
|
||||
# primeFactors.c:18: for (j = 2; j <= i / 2; j++) // Iterate over j from 2 to i/2
|
||||
lw a5,-20(s0) # tmp155, i
|
||||
srli a4,a5,31 #, tmp156, tmp155
|
||||
add a5,a4,a5 # tmp155, tmp157, tmp156
|
||||
srai a5,a5,1 #, _1, tmp157
|
||||
mv a4,a5 # _1, _1
|
||||
# primeFactors.c:18: for (j = 2; j <= i / 2; j++) // Iterate over j from 2 to i/2
|
||||
lw a5,-28(s0) # tmp159, j
|
||||
ble a5,a4,i_mod_set #, tmp159, _1,
|
||||
prime_check:
|
||||
# primeFactors.c:31: if (isPrime == 1) // Print i if i is a prime number
|
||||
lw a4,-36(s0) # tmp160, isPrime
|
||||
li a5,1 # tmp161,
|
||||
bne a4,a5,loop_i_inc #, tmp160, tmp161,
|
||||
prime_set:
|
||||
# primeFactors.c:33: Prime = i; // Set variable Prime to the prime number. Used for printing in the TB upon change
|
||||
lw a7,-20(s0) # tmp162, i
|
||||
sw a7,-44(s0) # tmp162, Prime
|
||||
loop_i_inc:
|
||||
# primeFactors.c:8: for (i = 2; i <= Number; i++) // Iterate over i from 2 to Number, all possible numbers excluding 1 & 0
|
||||
lw a5,-20(s0) # tmp164, i
|
||||
addi a5,a5,1 #, i_20, tmp164
|
||||
sw a5,-20(s0) # i_20, i
|
||||
loop_i_check:
|
||||
# primeFactors.c:8: for (i = 2; i <= Number; i++) // Iterate over i from 2 to Number, all possible numbers excluding 1 & 0
|
||||
lw a4,-20(s0) # tmp165, i
|
||||
lw a5,-40(s0) # tmp166, Number
|
||||
ble a4,a5,num_mod_set #, tmp165, tmp166,
|
||||
return:
|
||||
# primeFactors.c:37: return 0; // return the main function and return 0 on success
|
||||
li a5,0 # _10,
|
||||
# primeFactors.c:38: }
|
||||
mv a0,a5 #, <retval>
|
||||
lw ra,44(sp) # Load the return address from the stack and store it in ra
|
||||
lw s0,40(sp) # Load the caller frame pointer value from the stack and store it in s0
|
||||
addi sp,sp,48 # Free the allocated stack space for the function call
|
||||
jr ra # Return to address stored in ra
|
||||
@@ -0,0 +1,115 @@
|
||||
// *********************************************************************************************
|
||||
// Description : Simplified assembly instructions for prime factors program
|
||||
// Project Version : v1.0
|
||||
// Project : [BCDC] Microtec Academy Course: Building a RISC-V CPU with SystemVerilog
|
||||
// -----
|
||||
// Copyright (c) : 2025 Fraunhofer IIS, Department IDS
|
||||
// Created : 15.Oct.2025 by Hussein Elzomor
|
||||
// Last Modified : 21.Oct.2025 by Hussein Elzomor
|
||||
// -----
|
||||
// HISTORY : Date By Comments
|
||||
// ----------- --------- -------------------------------------------------
|
||||
// *********************************************************************************************
|
||||
|
||||
|
||||
|
||||
# 1 to 1 mapping of the Hex instructions in primeFactors.hex
|
||||
# Simplified version of primeFactors.s
|
||||
## Replaced pseudo instructions with the proper rv32i instructions
|
||||
## Replaced jumps to labels with address offset values
|
||||
|
||||
# Use as a cross reference to identify:
|
||||
## - the instruction being run on your processor
|
||||
## - which part of the program is being run
|
||||
## - confirm registers and memory addresses contain the correct data
|
||||
_start: # Hex # MemAddr(PC)
|
||||
lui x2 2 # 0x00002137 # 0x00000000
|
||||
addi x2 x2 -4 # 0xFFC10113 # 0x00000004
|
||||
jal x1 8 # 0x008000EF # 0x00000008
|
||||
halt:
|
||||
jal x0 0 # 0x0000006F # 0x0000000C
|
||||
main:
|
||||
addi x2 x2 -48 # 0xFD010113 # 0x00000010
|
||||
sw x1 44(x2) # 0x02112623 # 0x00000014
|
||||
sw x8 40(x2) # 0x02812423 # 0x00000018
|
||||
addi x8 x2 48 # 0x03010413 # 0x0000001C
|
||||
addi x15 x0 39 # 0x02700793 # 0x00000020 # Change the Number (39) to different values to test the program
|
||||
addi x16 x15 0 # 0x00078813 # 0x00000024 # Used for printing in the TB upon change
|
||||
sw x15 -40(x8) # 0xFCF42C23 # 0x00000028
|
||||
sw x0 -44(x8) # 0xFC042A23 # 0x0000002C
|
||||
loop_i_init:
|
||||
addi x15 x0 2 # 0x00200793 # 0x00000030
|
||||
sw x15 -20(x8) # 0xFEF42623 # 0x00000034
|
||||
jal x0 192 # 0x0C00006F # 0x00000038
|
||||
num_mod_set:
|
||||
lw x15 -40(x8) # 0xFD842783 # 0x0000003C
|
||||
sw x15 -32(x8) # 0xFEF42023 # 0x00000040
|
||||
jal x0 20 # 0x0140006F # 0x00000044
|
||||
num_mod_sub_i:
|
||||
lw x14 -32(x8) # 0xFE042703 # 0x00000048
|
||||
lw x15 -20(x8) # 0xFEC42783 # 0x0000004C
|
||||
sub x15 x14 x15 # 0x40F707B3 # 0x00000050
|
||||
sw x15 -32(x8) # 0xFEF42023 # 0x00000054
|
||||
num_mod_loop:
|
||||
lw x15 -32(x8) # 0xFE042783 # 0x00000058
|
||||
blt x0 x15 -20 # 0xFEF046E3 # 0x0000005C
|
||||
num_mod_check:
|
||||
lw x15 -32(x8) # 0xFE042783 # 0x00000060
|
||||
bne x15 x0 136 # 0x08079463 # 0x00000064
|
||||
addi x15 x0 1 # 0x00100793 # 0x00000068
|
||||
sw x15 -36(x8) # 0xFCF42E23 # 0x0000006C
|
||||
loop_j_init:
|
||||
addi x15 x0 2 # 0x00200793 # 0x00000070
|
||||
sw x15 -28(x8) # 0xFEF42223 # 0x00000074
|
||||
jal x0 68 # 0x0440006F # 0x00000078
|
||||
i_mod_set:
|
||||
lw x15 -20(x8) # 0xFEC42783 # 0x0000007C
|
||||
sw x15 -24(x8) # 0xFEF42423 # 0x00000080
|
||||
jal x0 20 # 0x0140006F # 0x00000084
|
||||
i_mod_sub_j:
|
||||
lw x14 -24(x8) # 0xFE842703 # 0x00000088
|
||||
lw x15 -28(x8) # 0xFE442783 # 0x0000008C
|
||||
sub x15 x14 x15 # 0x40F707B3 # 0x00000090
|
||||
sw x15 -24(x8) # 0xFEF42423 # 0x00000094
|
||||
i_mod_loop:
|
||||
lw x15 -24(x8) # 0xFE842783 # 0x00000098
|
||||
blt x0 x15 -20 # 0xFEF046E3 # 0x0000009C
|
||||
i_mod_check:
|
||||
lw x15 -24(x8) # 0xFE842783 # 0x000000A0
|
||||
bne x15 x0 12 # 0x00079663 # 0x000000A4
|
||||
sw x0 -36(x8) # 0xFC042E23 # 0x000000A8
|
||||
jal x0 44 # 0x02C0006F # 0x000000AC
|
||||
loop_j_inc:
|
||||
lw x15 -28(x8) # 0xFE442783 # 0x000000B0
|
||||
addi x15 x15 1 # 0x00178793 # 0x000000B4
|
||||
sw x15 -28(x8) # 0xFEF42223 # 0x000000B8
|
||||
loop_j_check:
|
||||
lw x15 -20(x8) # 0xFEC42783 # 0x000000BC
|
||||
srli x14 x15 31 # 0x01F7D713 # 0x000000C0
|
||||
add x15 x14 x15 # 0x00F707B3 # 0x000000C4
|
||||
srai x15 x15 1 # 0x4017D793 # 0x000000C8
|
||||
addi x14 x15 0 # 0x00078713 # 0x000000CC
|
||||
lw x15 -28(x8) # 0xFE442783 # 0x000000D0
|
||||
bge x14 x15 -88 # 0xFAF754E3 # 0x000000D4
|
||||
prime_check:
|
||||
lw x14 -36(x8) # 0xFDC42703 # 0x000000D8
|
||||
addi x15 x0 1 # 0x00100793 # 0x000000DC
|
||||
bne x14 x15 12 # 0x00F71663 # 0x000000E0
|
||||
prime_set:
|
||||
lw x17 -20(x8) # 0xFEC42883 # 0x000000E4
|
||||
sw x17 -44(x8) # 0xFD142A23 # 0x000000E8 # Used for printing in the TB upon change
|
||||
loop_i_inc:
|
||||
lw x15 -20(x8) # 0xFEC42783 # 0x000000EC
|
||||
addi x15 x15 1 # 0x00178793 # 0x000000F0
|
||||
sw x15 -20(x8) # 0xFEF42623 # 0x000000F4
|
||||
loop_i_check:
|
||||
lw x14 -20(x8) # 0xFEC42703 # 0x000000F8
|
||||
lw x15 -40(x8) # 0xFD842783 # 0x000000FC
|
||||
bge x15 x14 -196 # 0xF2E7DEE3 # 0x00000100
|
||||
return:
|
||||
addi x15 x0 0 # 0x00000793 # 0x00000104
|
||||
addi x10 x15 0 # 0x00078513 # 0x00000108
|
||||
lw x1 44(x2) # 0x02C12083 # 0x0000010C
|
||||
lw x8 40(x2) # 0x02812403 # 0x00000110
|
||||
addi x2 x2 48 # 0x03010113 # 0x00000114
|
||||
jalr x0 x1 0 # 0x00008067 # 0x00000118
|
||||
Reference in New Issue
Block a user