Skip to content

Commit

Permalink
old stale changes
Browse files Browse the repository at this point in the history
  • Loading branch information
iamlouk committed Aug 23, 2024
1 parent ec904bc commit 436cfcf
Show file tree
Hide file tree
Showing 6 changed files with 271 additions and 68 deletions.
4 changes: 2 additions & 2 deletions rust/examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ else
endif

%.elf: %.c
riscv64-elf-gcc -Wall -Werror -mabi=lp64 -march=rv64imac -g -O1 -static -o $@ $<
riscv64-elf-gcc -Wall -mabi=lp64 -march=rv64imac -g -O1 -static -o $@ $<

.PHONY: all clean

all: hello-world.elf bubblesort.elf nqueens.elf
all: hello-world.elf bubblesort.elf nqueens.elf grayscale.elf

clean:
rm -rf ./*.elf ./*.dump
172 changes: 172 additions & 0 deletions rust/examples/grayscale.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
#include <assert.h>
#include <errno.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static void die(const char msg[]) {
fprintf(stderr, "error(%s): %s\n", msg, strerror(errno));
exit(EXIT_FAILURE);
}

static inline uint8_t *encode_uint32(uint8_t *pos, uint32_t value) {
*(pos++) = value & 0xff;
*(pos++) = (value >> 8) & 0xff;
*(pos++) = (value >> 16) & 0xff;
*(pos++) = (value >> 24) & 0xff;
return pos;
}

struct color_palette {
uint8_t R;
uint8_t G;
uint8_t B;
uint8_t reserved;
};

static void print_grayscale_bmp(const char *filepath, size_t N, size_t M,
const uint8_t image[N][M]) {
FILE *f = NULL;
if (strcmp(filepath, "-") == 0) {
f = stdout;
} else {
f = fopen(filepath, "w");
if (!f)
die(filepath);
}

uint8_t header[54];
uint8_t *pos = &header[0];

/* BMP header */
*(pos++) = 'B';
*(pos++) = 'M';
pos = encode_uint32(pos, sizeof(header) +
N * M * sizeof(uint8_t)); // BMP file size
pos = encode_uint32(pos, 0x0); // unused
pos = encode_uint32(
pos,
sizeof(header) + 256 * sizeof(struct color_palette)); // pixel data offset

/* DIB header */
pos = encode_uint32(pos, 40); // DIB header length
pos = encode_uint32(pos, N); // height
pos = encode_uint32(pos, M); // width
*(pos++) = 1; // color Planes
*(pos++) = 0;
*(pos++) = 8; // bits per Pixel
*(pos++) = 0;
pos = encode_uint32(pos, 0); // Compression
pos = encode_uint32(pos, N * M * sizeof(uint8_t)); // RAW data size
pos = encode_uint32(pos, 0); // horizontal print resolution
pos = encode_uint32(pos, 0); // vertical print resolution
pos =
encode_uint32(pos, 0); // number of colors (0 is the default, means 2**N)
pos = encode_uint32(pos, 0); // important colors
assert(pos - 1 == &header[sizeof(header) - 1]);

if (fwrite(&header[0], sizeof(uint8_t), sizeof(header), f) != sizeof(header))
die("fwrite failed");

/* The color map. */
struct color_palette color_palette[256];
for (size_t i = 0; i < 256; i++) {
color_palette[i] = (struct color_palette){.R = i, .G = i, .B = i};
}

if (fwrite(&color_palette[0], sizeof(struct color_palette), 256, f) != 256)
die("fwrite failed");

/* The pixel data. */
if (fwrite(&image[0][0], sizeof(uint8_t), N * M, f) != N * M)
die("fwrite failed");

if (fflush(f) != 0)
die("fflush failed");

if (f != stdout && fclose(f) != 0)
die(filepath);
}

static int sqrti(int x) {
if (x == 0 || x == 1)
return x;

int i = 1, result = 1;
while (result <= x) {
i++;
result = i * i;
}
return i - 1;
}

static void draw_circle(size_t radius, int center_x, int center_y, size_t N,
size_t M, uint8_t image[N][M], uint8_t circle_color) {
for (size_t i = 0; i < N; i++) {
for (size_t j = 0; j < M; j++) {
int x = ((int)i - (int)(N / 2)) + center_x;
int y = ((int)j - (int)(M / 2)) + center_y;
int distance = sqrti(x * x + y * y);
if (distance < radius) {
image[i][j] = circle_color;
}
}
}
}

static void blur(size_t N, size_t M, const uint8_t image_src[N][M],
uint8_t image_dst[N][M]) {
for (size_t i = 1; i < N - 1; i++) {
for (size_t j = 1; j < M - 1; j++) {
uint64_t pixel = image_src[i - 1][j - 1] + image_src[i - 1][j - 0] +
image_src[i - 1][j + 1] + image_src[i - 0][j - 1] +
image_src[i - 0][j - 0] + image_src[i - 0][j + 1] +
image_src[i + 1][j - 1] + image_src[i + 1][j - 0] +
image_src[i + 1][j + 1];
image_dst[i][j] = (uint8_t)(pixel / 9);
}
}
}

__attribute__((noinline)) static void init(size_t N, size_t M,
uint8_t (*image)[M], uint8_t val) {
for (size_t i = 0; i < N; i++)
for (size_t j = 0; j < M; j++)
image[i][j] = val;
}

int main() {
// fprintf(stderr, "debug: main started...\n");

size_t N = 500;
size_t M = 500;
uint8_t(*image)[M] = malloc(N * M * sizeof(uint8_t));

init(N, M, image, 0x00);

// fprintf(stderr, "debug: image allocated...\n");
// fprintf(stderr, "debug: image initialized...\n");

#if 1
draw_circle(150, 100, 100, N, M, image, 0xff);
draw_circle(100, -50, -100, N, M, image, 0xb0);
draw_circle(200, 200, -100, N, M, image, 0x80);
draw_circle(100, -50, 100, N, M, image, 0x40);
draw_circle(50, 50, 200, N, M, image, 0xb0);
draw_circle(200, -250, -250, N, M, image, 0x80);
fprintf(stderr, "debug: circles drawn...\n");

uint8_t(*tmp_image)[M] = malloc(N * M * sizeof(uint8_t));
for (size_t i = 0; i < 10; i++) {
blur(N, M, image, tmp_image);
blur(N, M, tmp_image, image);
}
fprintf(stderr, "debug: blured...\n");

print_grayscale_bmp("-", N, M, image);
fprintf(stderr, "debug: done!\n");
#endif
return EXIT_SUCCESS;
}
2 changes: 1 addition & 1 deletion rust/src/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl CPU {
}

/* TODO: There must be a better way... What address to use as TOS? */
let top_of_stack = 0x10000u64 as usize;
let top_of_stack = MAX_ADDR - (MAX_ADDR >> 2);
self.set_reg(REG_SP, top_of_stack as u64);
if let Some(argv) = argv {
/*
Expand Down
9 changes: 6 additions & 3 deletions rust/src/insts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,17 @@ pub enum RoundingMode {
#[derive(Debug, Clone)]
pub enum Inst {
Unknown,
NOP,
NOP, // RV does not actually have a NOP, but its still usefull as explicit entry.

// Not even the non-privileged CSRs are implemented yet.
#[allow(dead_code)]
CtrlStatusReg { op: CSR, dst: Reg, src: Reg, csr: u16 },

Load { dst: Reg, width: u8, base: Reg, offset: i32, signext: bool },
Store { src: Reg, width: u8, base: Reg, offset: i32 },
JumpAndLink { dst: Reg, offset: i32 },
JumpAndLinkReg { dst: Reg, base: Reg, offset: i32 },
Branch { pred: Predicate, src1: Reg, src2: Reg, offset: i32 },
CtrlStatusReg { op: CSR, dst: Reg, src: Reg, csr: u16 },
ECall { _priv: u8 },
EBreak { _priv: u8 },
ALUImm { op: ALU, dst: Reg, src1: Reg, imm: u32 },
Expand Down Expand Up @@ -759,4 +763,3 @@ impl Inst {
Inst::JumpAndLinkReg { dst: REG_ZR, base: REG_RA, offset: 0 })
}
}

Loading

0 comments on commit 436cfcf

Please sign in to comment.