URL
https://opencores.org/ocsvn/potato/potato/trunk
Subversion Repositories potato
Compare Revisions
- This comparison shows the changes necessary to convert path
/potato/tags/v0.1/benchmarks/sha256
- from Rev 20 to Rev 47
- ↔ Reverse comparison
Rev 20 → Rev 47
/main.c
0,0 → 1,96
// The Potato Processor Benchmark Applications |
// (c) Kristian Klomsten Skordal 2015 <kristian.skordal@wafflemail.net> |
// Report bugs and issues on <http://opencores.org/project,potato,bugtracker> |
|
#include <stdbool.h> |
#include <stdint.h> |
|
#include "platform.h" |
#include "potato.h" |
|
#include "gpio.h" |
#include "sha256.h" |
#include "timer.h" |
#include "uart.h" |
|
static int led_status = 0; |
static volatile int hashes_per_second = 0; |
|
// Handle an exception/interrupt. |
// Arguments: |
// - cause: exception cause, see potato.h for values |
// - epc: exception return address |
// - regbase: base of the stored context, can be used for printing all |
// registers with regbase[0] = x1 and upwards. |
void exception_handler(uint32_t cause, void * epc, void * regbase) |
{ |
if(cause == (CAUSE_IRQ_BASE + 5)) // Timer interrupt |
{ |
uart_puts(IO_ADDRESS(UART_BASE), "Hashes per second: "); |
uart_puth(IO_ADDRESS(UART_BASE), hashes_per_second); |
uart_puts(IO_ADDRESS(UART_BASE), "\n\r"); |
|
if(led_status == 0) |
{ |
gpio_set_output(IO_ADDRESS(GPIO2_BASE), 1); |
led_status = 1; |
} else { |
gpio_set_output(IO_ADDRESS(GPIO2_BASE), 0); |
led_status = 0; |
} |
|
hashes_per_second = 0; |
timer_reset(IO_ADDRESS(TIMER_BASE)); |
} else { |
uart_puts(IO_ADDRESS(UART_BASE), "Unhandled exception!\n\r"); |
uart_puts(IO_ADDRESS(UART_BASE), "Cause: "); |
uart_puth(IO_ADDRESS(UART_BASE), cause); |
uart_puts(IO_ADDRESS(UART_BASE), "\n\r"); |
uart_puts(IO_ADDRESS(UART_BASE), "EPC: "); |
uart_puth(IO_ADDRESS(UART_BASE), (uint32_t) epc); |
uart_puts(IO_ADDRESS(UART_BASE), "\n\r"); |
|
while(1) asm volatile("nop\n"); |
} |
} |
|
int main(void) |
{ |
// Configure GPIOs: |
gpio_set_direction(IO_ADDRESS(GPIO1_BASE), 0x0000); // Switches |
gpio_set_direction(IO_ADDRESS(GPIO2_BASE), 0xffff); // LEDs |
|
// Set up the timer: |
timer_set(IO_ADDRESS(TIMER_BASE), SYSTEM_CLK_FREQ); |
|
// Print a startup message: |
uart_puts(IO_ADDRESS(UART_BASE), "The Potato Processor SHA256 Benchmark\n\r\n\r"); |
|
// Enable interrupts: |
potato_enable_irq(TIMER_IRQ); |
potato_enable_interrupts(); |
|
struct sha256_context context; |
|
// Prepare a block for hashing: |
uint32_t block[16]; |
uint8_t * block_ptr = (uint8_t *) block; |
block_ptr[0] = 'a'; |
block_ptr[1] = 'b'; |
block_ptr[2] = 'c'; |
sha256_pad_le_block(block_ptr, 3, 3); |
|
// Repeatedly hash the same data over and over again: |
while(true) |
{ |
uint8_t hash[32]; |
|
sha256_reset(&context); |
sha256_hash_block(&context, block); |
sha256_get_hash(&context, hash); |
++hashes_per_second; |
} |
|
return 0; |
} |
|
/Makefile
0,0 → 1,61
# The Potato Processor Benchmark Applications |
# (c) Kristian Klomsten Skordal 2015 <kristian.skordal@wafflemail.net> |
# Report bugs and issues on <http://opencores.org/project,potato,bugtracker> |
|
.PHONY: all clean |
|
TARGET_PREFIX ?= riscv64-unknown-elf |
TARGET_CC := $(TARGET_PREFIX)-gcc |
TARGET_LD := $(TARGET_PREFIX)-ld |
TARGET_SIZE := $(TARGET_PREFIX)-size |
TARGET_OBJCOPY := $(TARGET_PREFIX)-objcopy |
HEXDUMP ?= hexdump |
|
TARGET_CFLAGS += -m32 -march=RV32I -Wall -Os -fomit-frame-pointer \ |
-ffreestanding -fno-builtin -I.. -std=gnu99 |
TARGET_LDFLAGS += -m elf32lriscv -T../benchmark.ld |
|
OBJECTS := gpio.o main.o sha256.o start.o timer.o uart.o utilities.o |
|
all: sha256.coe |
|
sha256.elf: $(OBJECTS) |
$(TARGET_LD) -o sha256.elf $(TARGET_LDFLAGS) $(OBJECTS) |
$(TARGET_SIZE) sha256.elf |
|
sha256.bin: sha256.elf |
$(TARGET_OBJCOPY) -j .text -j .data -O binary sha256.elf sha256.bin |
|
sha256.coe: sha256.bin |
echo "memory_initialization_radix=16;" > sha256.coe |
echo "memory_initialization_vector=" >> sha256.coe |
$(HEXDUMP) -v -e '1/4 "%08x\n"' sha256.bin >> sha256.coe |
echo ";" >> sha256.coe |
|
clean: |
-$(RM) $(OBJECTS) |
-$(RM) sha256.elf sha256.bin sha256.coe |
|
# Object file rules: |
|
gpio.o: gpio.c gpio.h ../platform.h |
$(TARGET_CC) -c -o $@ $(TARGET_CFLAGS) $< |
|
main.o: main.c gpio.h timer.h sha256.h ../platform.h ../potato.h |
$(TARGET_CC) -c -o $@ $(TARGET_CFLAGS) $< |
|
sha256.o: sha256.c sha256.h |
$(TARGET_CC) -c -o $@ $(TARGET_CFLAGS) $< |
|
start.o: ../start.S ../platform.h |
$(TARGET_CC) -c -o $@ $(TARGET_CFLAGS) $< |
|
timer.o: timer.c timer.h ../platform.h |
$(TARGET_CC) -c -o $@ $(TARGET_CFLAGS) $< |
|
uart.o: uart.c uart.h ../platform.h |
$(TARGET_CC) -c -o $@ $(TARGET_CFLAGS) $< |
|
utilities.o: utilities.c utilities.h |
$(TARGET_CC) -c -o $@ $(TARGET_CFLAGS) $< |
|
/sha256.c
0,0 → 1,167
// The Potato Processor Benchmark Applications |
// (c) Kristian Klomsten Skordal 2015 <kristian.skordal@wafflemail.net> |
// Report bugs and issues on <http://opencores.org/project,potato,bugtracker> |
|
#include "platform.h" |
#include "gpio.h" |
|
#include "sha256.h" |
|
#define htobe32(n) ((uint32_t) ((n << 24) | ((n << 8) & 0xff0000) | ((n >> 8) & 0xff00) | (n >> 24))) |
|
// Software SHA256 module |
|
static const uint32_t initial[8] = |
{ |
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, |
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 |
}; |
|
static const uint32_t constants[64] = |
{ |
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, |
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, |
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, |
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, |
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, |
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, |
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, |
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 |
}; |
|
static uint32_t rotate_right(uint32_t x, int n) |
{ |
return (x >> n) | (x << (32 - n)); |
} |
|
static uint32_t Ch(uint32_t x, uint32_t y, uint32_t z) |
{ |
return (x & y) ^ ((~x) & z); |
} |
|
static uint32_t Maj(uint32_t x, uint32_t y, uint32_t z) |
{ |
return (x & y) ^ (x & z) ^ (y & z); |
} |
|
static uint32_t s0(uint32_t x) |
{ |
return rotate_right(x, 2) ^ rotate_right(x, 13) ^ rotate_right(x, 22); |
} |
|
static uint32_t s1(uint32_t x) |
{ |
return rotate_right(x, 6) ^ rotate_right(x, 11) ^ rotate_right(x, 25); |
} |
|
static uint32_t o0(uint32_t x) |
{ |
return rotate_right(x, 7) ^ rotate_right(x, 18) ^ (x >> 3); |
} |
|
static uint32_t o1(uint32_t x) |
{ |
return rotate_right(x, 17) ^ rotate_right(x, 19) ^ (x >> 10); |
} |
|
static uint32_t schedule(uint32_t input, const uint32_t * W, int i) |
{ |
if(i < 16) |
return input; |
else |
return o1(W[i - 2]) + W[i - 7] + o0(W[i - 15]) + W[i - 16]; |
} |
|
static void compress(uint32_t * i, uint32_t W, uint32_t K) |
{ |
uint32_t a = i[0], b = i[1], c = i[2], d = i[3]; |
uint32_t e = i[4], f = i[5], g = i[6], h = i[7]; |
|
uint32_t t1 = h + s1(e) + Ch(e, f, g) + K + W; |
uint32_t t2 = s0(a) + Maj(a, b, c); |
|
h = g; |
g = f; |
f = e; |
e = d + t1; |
d = c; |
c = b; |
b = a; |
a = t1 + t2; |
|
i[0] = a; |
i[1] = b; |
i[2] = c; |
i[3] = d; |
i[4] = e; |
i[5] = f; |
i[6] = g; |
i[7] = h; |
} |
|
void sha256_reset(struct sha256_context * ctx) |
{ |
for(int i = 0; i < 8; ++i) |
ctx->intermediate[i] = initial[i]; |
} |
|
void sha256_hash_block(struct sha256_context * ctx, const uint32_t * data) |
{ |
uint32_t W[64]; |
uint32_t temp[8]; |
|
for(int i = 0; i < 8; ++i) |
temp[i] = ctx->intermediate[i]; |
|
for(int i = 0; i < 64; ++i) |
{ |
uint32_t v = i < 16 ? data[i] : 0; |
W[i] = schedule(v, W, i); |
compress(temp, W[i], constants[i]); |
} |
|
for(int i = 0; i < 8; ++i) |
ctx->intermediate[i] += temp[i]; |
} |
|
void sha256_pad_le_block(uint8_t * block, int block_length, uint64_t total_length) |
{ |
block[block_length] = 0x80; // Add a one to the end of the message; |
for(int i = block_length + 1; i < 64; ++i) |
block[i] = 0; |
|
((uint32_t *) block)[14] = total_length * 8 >> 32; |
((uint32_t *) block)[15] = total_length * 8 & 0xffffffff; |
|
// Convert the block to big-endian: |
for(int i = 0; i < 14; ++i) |
((uint32_t *) block)[i] = htobe32(((uint32_t *) block)[i]); |
} |
|
void sha256_get_hash(const struct sha256_context * ctx, uint8_t * hash) |
{ |
for(int i = 0; i < 8; ++i) |
{ |
// Return the hash in little-endian format: |
hash[i * 4 + 3] = (ctx->intermediate[i] >> 0) & 0xff; |
hash[i * 4 + 2] = (ctx->intermediate[i] >> 8) & 0xff; |
hash[i * 4 + 1] = (ctx->intermediate[i] >> 16) & 0xff; |
hash[i * 4 + 0] = (ctx->intermediate[i] >> 24) & 0xff; |
} |
} |
|
void sha256_format_hash(const uint8_t * hash, char * output) |
{ |
static const char * hex_digits = "0123456789abcdef"; |
for(int i = 0; i < 32; i++) |
{ |
uint8_t h = hash[i]; |
|
output[i * 2 + 0] = hex_digits[(h >> 4) & 0xf]; |
output[i * 2 + 1] = hex_digits[h & 0xf]; |
} |
|
output[64] = 0; |
} |
|
|
/gpio.c
0,0 → 1,23
// The Potato Processor Benchmark Applications |
// (c) Kristian Klomsten Skordal 2015 <kristian.skordal@wafflemail.net> |
// Report bugs and issues on <http://opencores.org/project,potato,bugtracker> |
|
#include "gpio.h" |
#include "platform.h" |
|
void gpio_set_direction(volatile uint32_t * base, uint32_t direction) |
{ |
base[GPIO_DIR >> 2] = direction; |
} |
|
void gpio_set_output(volatile uint32_t * base, uint32_t output) |
{ |
base[GPIO_OUTPUT >> 2] = output; |
} |
|
uint32_t gpio_get_value(volatile uint32_t * base) |
{ |
return base[GPIO_INPUT >> 2]; |
} |
|
|
/utilities.h
0,0 → 1,11
// The Potato Processor Benchmark Applications |
// (c) Kristian Klomsten Skordal 2015 <kristian.skordal@wafflemail.net> |
// Report bugs and issues on <http://opencores.org/project,potato,bugtracker> |
|
#ifndef UTILITIES_H |
#define UTILITIES_H |
|
void * memset(void * s, int c, int n); |
|
#endif |
|
/uart.h
0,0 → 1,17
// The Potato Processor Benchmark Applications |
// (c) Kristian Klomsten Skordal 2015 <kristian.skordal@wafflemail.net> |
// Report bugs and issues on <http://opencores.org/project,potato,bugtracker> |
|
#ifndef UART_H |
#define UART_H |
|
#include <stdint.h> |
|
// TODO: Implement the M extension and then write a printf function. |
|
void uart_puts(volatile uint32_t * base, const char * s); |
void uart_putc(volatile uint32_t * base, char c); |
void uart_puth(volatile uint32_t * base, uint32_t n); |
|
#endif |
|
/timer.c
0,0 → 1,18
// The Potato Processor Benchmark Applications |
// (c) Kristian Klomsten Skordal 2015 <kristian.skordal@wafflemail.net> |
// Report bugs and issues on <http://opencores.org/project,potato,bugtracker> |
|
#include "platform.h" |
#include "timer.h" |
|
void timer_set(volatile uint32_t * base, uint32_t compare) |
{ |
base[TIMER_COMPARE >> 2] = compare; |
timer_reset(base); |
} |
|
void timer_reset(volatile uint32_t * base) |
{ |
base[TIMER_CTRL >> 2] = 1 << TIMER_CTRL_RUN | 1 << TIMER_CTRL_CLEAR; |
} |
|
/gpio.h
0,0 → 1,18
// The Potato Processor Benchmark Applications |
// (c) Kristian Klomsten Skordal 2015 <kristian.skordal@wafflemail.net> |
// Report bugs and issues on <http://opencores.org/project,potato,bugtracker> |
|
#ifndef GPIO_H |
#define GPIO_H |
|
#include <stdint.h> |
|
#define DIRECTION_INPUT 0 |
#define DIRECTION_OUTPUT 1 |
|
void gpio_set_direction(volatile uint32_t * base, uint32_t direction); |
void gpio_set_output(volatile uint32_t * base, uint32_t output); |
uint32_t gpio_get_value(volatile uint32_t * base); |
|
#endif |
|
/timer.h
0,0 → 1,14
// The Potato Processor Benchmark Applications |
// (c) Kristian Klomsten Skordal 2015 <kristian.skordal@wafflemail.net> |
// Report bugs and issues on <http://opencores.org/project,potato,bugtracker> |
|
#ifndef TIMER_H |
#define TIMER_H |
|
#include <stdint.h> |
|
void timer_set(volatile uint32_t * base, uint32_t compare); |
void timer_reset(volatile uint32_t * base); |
|
#endif |
|
/utilities.c
0,0 → 1,14
// The Potato Processor Benchmark Applications |
// (c) Kristian Klomsten Skordal 2015 <kristian.skordal@wafflemail.net> |
// Report bugs and issues on <http://opencores.org/project,potato,bugtracker> |
|
#include "utilities.h" |
|
void * memset(void * s, int c, int n) |
{ |
char * temp = s; |
for(int i = 0; i < n; ++i) |
temp[i] = c; |
return s; |
} |
|
/uart.c
0,0 → 1,32
// The Potato Processor Benchmark Applications |
// (c) Kristian Klomsten Skordal 2015 <kristian.skordal@wafflemail.net> |
// Report bugs and issues on <http://opencores.org/project,potato,bugtracker> |
|
#include <stdarg.h> |
#include <stdbool.h> |
|
#include "platform.h" |
#include "uart.h" |
|
void uart_puts(volatile uint32_t * base, const char * s) |
{ |
for(int i = 0; s[i] != 0; ++i) |
uart_putc(base, s[i]); |
} |
|
void uart_putc(volatile uint32_t * base, char c) |
{ |
// Wait until there is room in the transmit buffer: |
while(base[UART_STATUS >> 2] & (1 << 3)); |
base[UART_TX >> 2] = c & 0x000000ff; |
} |
|
void uart_puth(volatile uint32_t * base, uint32_t n) |
{ |
static const char * hex_digits = "0123456789abcdef"; |
uart_putc(base, '0'); |
uart_putc(base, 'x'); |
for(int i = 28; i >= 0; i -= 4) |
uart_putc(base, hex_digits[(n >> i) & 0xf]); |
} |
|
/sha256.h
0,0 → 1,31
// The Potato Processor Benchmark Applications |
// (c) Kristian Klomsten Skordal 2015 <kristian.skordal@wafflemail.net> |
// Report bugs and issues on <http://opencores.org/project,potato,bugtracker> |
|
#ifndef SHA256_H |
#define SHA256_H |
|
#include <stdint.h> |
|
struct sha256_context |
{ |
uint32_t intermediate[8]; |
}; |
|
// Resets a SHA256 context: |
void sha256_reset(struct sha256_context * ctx); |
|
// Hash a block of data: |
void sha256_hash_block(struct sha256_context * ctx, const uint32_t * data); |
|
// Pad a block of data to hash: |
void sha256_pad_le_block(uint8_t * block, int block_length, uint64_t total_length); |
|
// Get the hash from a SHA256 context: |
void sha256_get_hash(const struct sha256_context * ctx, uint8_t * hash); |
|
// Formats a hash for printing: |
void sha256_format_hash(const uint8_t * hash, char * output); |
|
#endif |
|