Skip to content

Commit

Permalink
Update firmware demo code
Browse files Browse the repository at this point in the history
  • Loading branch information
hl271 committed Dec 8, 2023
1 parent 0e4e122 commit a7c4ff2
Show file tree
Hide file tree
Showing 7 changed files with 312 additions and 63 deletions.
2 changes: 1 addition & 1 deletion verilog/dv/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
.SILENT: clean all


PATTERNS = io_ports la_test1 la_test2 wb_port mprj_stimulus
PATTERNS = io_ports la_test1 la_test2 wb_port mprj_stimulus edabk_firmware_demo

all: ${PATTERNS}

Expand Down
26 changes: 0 additions & 26 deletions verilog/dv/edabk_firmware_demo/SNN_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,6 @@

#include "SNN_data.h"

#define NUM_CORES 5
#define NEURONS_PER_CORE 32
#define AXONS_PER_CORE 256

typedef struct {
int8_t membrane_potential;
int8_t reset_posi_potential;
int8_t reset_nega_potential;
int8_t weights[4];
int8_t leakage_value;
int8_t positive_threshold;
int8_t negative_threshold;
uint8_t axon_dest;
} Neuron;

typedef struct {
Neuron neurons[NEURONS_PER_CORE];
uint32_t synapse_connection[AXONS_PER_CORE];
} Core;

typedef struct {
int8_t dx;
int8_t dy;
uint8_t axon_dest;
} Packet;

Core cores[NUM_CORES] = {
{ // Core 0
.neurons = {
Expand Down
30 changes: 28 additions & 2 deletions verilog/dv/edabk_firmware_demo/SNN_data.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
#ifndef SNN_DATA_H
#define SNN_DATA_H

extern Packet packet_data[NUM_CORES][1000];
extern Core core_data[NUM_CORES];
#define NUM_CORES 5
#define NEURONS_PER_CORE 32
#define AXONS_PER_CORE 256

typedef struct {
int8_t membrane_potential;
int8_t reset_posi_potential;
int8_t reset_nega_potential;
int8_t weights[4];
int8_t leakage_value;
int8_t positive_threshold;
int8_t negative_threshold;
uint8_t axon_dest;
} Neuron;

typedef struct {
Neuron neurons[NEURONS_PER_CORE];
uint32_t synapse_connection[AXONS_PER_CORE];
} Core;

typedef struct {
int8_t dx;
int8_t dy;
uint8_t axon_dest;
} Packet;

extern Packet packets[1024];
extern Core cores[NUM_CORES];

#endif
43 changes: 18 additions & 25 deletions verilog/dv/edabk_firmware_demo/convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,24 @@ unsigned int bitStringToUnsignedInt(char *str, int start, int end) {
return value;
}

// Function to convert a string of bits to a signed integer
int bitStringToSignedInt(char *str, int start, int end) {
int value = 0;
int sign = str[start] == '1' ? -1 : 1;
for (int i = start; i < end; i++) {
value = value * 2 + (str[i] - '0');
}
return sign == -1 ? -(value & ~(1 << (end - start - 1))) : value;
}

// Function to convert a string of bits to an unsigned integer
unsigned int bitStringToUnsignedInt(char *str, int start, int end) {
unsigned int value = 0;
for (int i = start; i < end; i++) {
value = value * 2 + (str[i] - '0');
}
return value;
}
// // Function to convert a string of bits to a signed integer
// int bitStringToSignedInt(char *str, int start, int end) {
// int value = 0;
// int sign = str[start] == '1' ? -1 : 1;
// for (int i = start; i < end; i++) {
// value = value * 2 + (str[i] - '0');
// }
// return sign == -1 ? -(value & ~(1 << (end - start - 1))) : value;
// }

// // Function to convert a string of bits to an unsigned integer
// unsigned int bitStringToUnsignedInt(char *str, int start, int end) {
// unsigned int value = 0;
// for (int i = start; i < end; i++) {
// value = value * 2 + (str[i] - '0');
// }
// return value;
// }

int main() {
FILE *inputFile1, *inputFile2, *outputFile;
Expand Down Expand Up @@ -217,13 +217,6 @@ int main() {

fprintf(outputFile, "Packet packets[%d] = {\n", packetCount);
for (int i = 0; i < packetCount; i++) {
// Packet &p = packets[i]; // C doesn't have references, cannot use!!!

// //Debug
// for (int i=0; i< 10; i++) {
// printf("Packet %d: {}")
// }

fprintf(outputFile, " { %u, %d, %d },\n", packets[i].dx, packets[i].dy, packets[i].axon_dest);
}
fprintf(outputFile, "};\n");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@


/* User Project Memory Mapping
=> the #define directives help in creating symbolic names for memory addresses
Expand All @@ -11,11 +13,22 @@
| 0x300040F0 - 0x300040FB| param31 |
| 0x30008000 - 0x30008003| neuron_spike_out |
*/

// This include is relative to $CARAVEL_PATH (see Makefile)
#include <defs.h>
#include <stub.c>

#include <stdint.h>
#include "SNN_data.h"

#define NUM_CORES 5
#define NEURONS_PER_CORE 32
#define AXONS_PER_CORE 256

#define SYNAP_MATRIX_BASE 0x30000000
#define PARAM_BASE 0x30004000
#define NEURON_SPIKE_OUT_BASE 0x30008000

#include "SNN_data.h"

/* Pointers for User Project Memory Mapping
- Each pointer is assigned to the corresponding user project address on the wishbone bus
Expand Down Expand Up @@ -46,7 +59,7 @@ void send_synapse_connection_to_mem(uint8_t core_index, volatile uint32_t* base_

// Send synapse connection data to memory in 32-bit batch
for (uint32_t i = 0; i < AXONS_PER_CORE; ++i) {
uint32_t synapse_connection_value = core_data[core_index].synapse_connection[i];
uint32_t synapse_connection_value = cores[core_index].synapse_connection[i];
write_32_bit_to_mem(base_addr, offset + i * 4, synapse_connection_value);
}
}
Expand All @@ -59,7 +72,7 @@ void send_neuron_params_to_mem(uint8_t core_index, volatile uint32_t* base_addr)

// Send neuron parameters to memory in 32-bit batches
for (uint32_t i = 0; i < NEURONS_PER_CORE; ++i) {
Neuron* current_neuron = &core_data[core_index].neurons[i];
Neuron* current_neuron = &cores[core_index].neurons[i];

// Calculate the offset for the current neuron
uint32_t neuron_offset = offset + i * 11;
Expand Down Expand Up @@ -94,15 +107,15 @@ void read_neuron_params_from_mem(uint8_t core_index, volatile uint32_t* base_add

// Read neuron parameters from memory in 32-bit batches
for (uint32_t i = 0; i < NEURONS_PER_CORE; ++i) {
Neuron* current_neuron = &core_data[core_index].neurons[i];
Neuron* current_neuron = &cores[core_index].neurons[i];

// Calculate the offset for the current neuron
uint32_t neuron_offset = offset + i * 11 * 4; // Multiply by 4 for each 32-bit word
uint32_t neuron_offset = offset + i * 11;

// Read the concatenated batches from memory
uint32_t batch1 = read32Batch(base_addr + neuron_offset, 0);
uint32_t batch2 = read32Batch(base_addr + neuron_offset + 1, 0);
uint32_t batch3 = read32Batch(base_addr + neuron_offset + 2, 0);
uint32_t batch1 = read_32bit_from_mem(base_addr + neuron_offset, 0);
uint32_t batch2 = read_32bit_from_mem(base_addr + neuron_offset + 4, 0);
uint32_t batch3 = read_32bit_from_mem(base_addr + neuron_offset + 8, 0);

// Extract individual parameters from batches
current_neuron->leakage_value = (int8_t)(batch1 >> 24);
Expand Down Expand Up @@ -167,5 +180,6 @@ void main() {
reg_mprj_datal = 0xAB600000;

/* SEND NEURON_DATA OF A CORE TO MEM */

send_synapse_connection_to_mem(0, SYNAP_MATRIX_PTR);
reg_mprj_datal = 0xAB610000;
}
Loading

0 comments on commit a7c4ff2

Please sign in to comment.