Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mmio record #930

Merged
merged 7 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions .github/designs/blastoise/t1rocketemu.json
Original file line number Diff line number Diff line change
Expand Up @@ -512,17 +512,17 @@
"mlir.rvv_vp_intrinsic_add_scalable": 1008,
"mlir.stripmining": 9242,
"mlir.vectoradd": 13344,
"pytorch.demo": 33242,
"pytorch.matmul": 73186,
"rvv_bench.ascii_to_utf16": 702304,
"rvv_bench.ascii_to_utf32": 235872,
"rvv_bench.byteswap": 407420,
"rvv_bench.chacha20": 40757,
"rvv_bench.mandelbrot": 557756,
"rvv_bench.memcpy": 709571,
"rvv_bench.memset": 295866,
"rvv_bench.mergelines": 577082,
"rvv_bench.poly1305": 40757,
"rvv_bench.strlen": 220022,
"rvv_bench.utf8_count": 2304110
"pytorch.demo": 33202,
"pytorch.matmul": 73102,
"rvv_bench.ascii_to_utf16": 701692,
"rvv_bench.ascii_to_utf32": 235679,
"rvv_bench.byteswap": 407359,
"rvv_bench.chacha20": 40800,
"rvv_bench.mandelbrot": 557777,
"rvv_bench.memcpy": 709378,
"rvv_bench.memset": 295845,
"rvv_bench.mergelines": 577117,
"rvv_bench.poly1305": 40800,
"rvv_bench.strlen": 220857,
"rvv_bench.utf8_count": 2304128
}
20 changes: 10 additions & 10 deletions .github/designs/rookidee/t1rocketemu.json
Original file line number Diff line number Diff line change
Expand Up @@ -435,14 +435,14 @@
"mlir.rvv_vp_intrinsic_add": 606,
"mlir.rvv_vp_intrinsic_add_scalable": 896,
"mlir.stripmining": 28954,
"rvv_bench.ascii_to_utf16": 704963,
"rvv_bench.ascii_to_utf32": 236223,
"rvv_bench.byteswap": 445945,
"rvv_bench.chacha20": 40757,
"rvv_bench.memcpy": 716867,
"rvv_bench.memset": 301885,
"rvv_bench.mergelines": 595373,
"rvv_bench.poly1305": 40757,
"rvv_bench.strlen": 239291,
"rvv_bench.utf8_count": 2377771
"rvv_bench.ascii_to_utf16": 704310,
"rvv_bench.ascii_to_utf32": 235978,
"rvv_bench.byteswap": 445875,
"rvv_bench.chacha20": 40800,
"rvv_bench.memcpy": 716655,
"rvv_bench.memset": 301824,
"rvv_bench.mergelines": 595465,
"rvv_bench.poly1305": 40800,
"rvv_bench.strlen": 240136,
"rvv_bench.utf8_count": 2377786
}
40 changes: 34 additions & 6 deletions difftest/dpi_t1rocketemu/src/interconnect/simctrl.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
use std::sync::{
atomic::{AtomicU32, Ordering},
Arc,
use std::{
fs::File,
io::Write as _,
sync::{
atomic::{AtomicU32, Ordering},
Arc,
},
};

use tracing::{info, warn};

use crate::get_t;

use super::RegDevice;

#[derive(Default, Debug, Clone)]
Expand All @@ -28,20 +34,36 @@ pub const EXIT_CODE: u32 = 0xdead_beef;

/// Reg map:
/// - 0x0000 : WO, write EXIT_CODE to mark simulation finish
/// - 0x0010 : WO, uart write register
/// - 0x0014 : WO, profile write register
///
/// Event file:
/// all writes to uart/profile write register are recorded blindly
/// to "mmio-event.jsonl"
pub struct SimCtrl {
exit_flag: ExitFlagRef,
event_file: File,
}

impl SimCtrl {
pub fn new(exit_flag: ExitFlagRef) -> Self {
SimCtrl { exit_flag }
let event_file = File::create("mmio-event.jsonl").unwrap();
SimCtrl { exit_flag, event_file }
}

fn append_event(&mut self, event: &str, value: u32) {
let cycle = get_t();
let buf = format!("{{\"cycle\": {cycle}, \"event\": \"{event}\", \"value\": {value}}}\n");

self.event_file.write_all(buf.as_bytes()).unwrap();
self.event_file.flush().unwrap();
}
}

impl RegDevice for SimCtrl {
fn reg_read(&mut self, offset: u32) -> u32 {
let _ = offset;
unimplemented!()
unimplemented!("simctrl does not support mmio read")
}

fn reg_write(&mut self, reg_offset: u32, value: u32) {
Expand All @@ -54,7 +76,13 @@ impl RegDevice for SimCtrl {
warn!("simctrl: write EXIT_POS with value 0x{value:08x}, ignored");
}
}
_ => panic!(),
0x10 => {
self.append_event("uart-write", value);
}
0x14 => {
self.append_event("profile", value);
}
_ => panic!("simctrl: invalid write addr: base + 0x{reg_offset:02x}"),
}
}
}
4 changes: 4 additions & 0 deletions nix/t1/run/run-vcs-emu.nix
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ stdenvNoCC.mkDerivation (rec {
mv perf.json $out/
fi

if [ -r mmio-event.jsonl ]; then
cp -v mmio-event.jsonl $out/
fi

${lib.optionalString emulator.enableCover ''
${snps-fhs-env}/bin/snps-fhs-env -c "urg -dir cm.vdb -format text -metric assert -show summary"
# TODO: add a flag to specify 'vdb only generated in ci mode'
Expand Down
3 changes: 2 additions & 1 deletion tests/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,12 @@ let
rvv_bench = casesSelf.callPackage ./rvv_bench { };
pytorch = casesSelf.callPackage ./pytorch { };
disp = casesSelf.callPackage ./disp { };
emurt-test = casesSelf.callPackage ./emurt/tests { };
}));

# remove non-case attributes in scope
scopeStripped = {
inherit (scope) mlir intrinsic asm perf codegen rvv_bench pytorch disp;
inherit (scope) mlir intrinsic asm perf codegen rvv_bench pytorch disp emurt-test;
};

# This derivation is for internal CI use only.
Expand Down
92 changes: 21 additions & 71 deletions tests/emurt/emurt.c
Original file line number Diff line number Diff line change
@@ -1,54 +1,19 @@
#include "emurt.h"

#define EXIT_REG 0x10000000
#define UART_W_REG 0x10000010
#define PERF_REG 0x10000014

void t1_put_char(char c) { *(uint8_t volatile *)(UART_W_REG) = (uint8_t)c; }
void place_counter(int i) { *(int volatile *)(PERF_REG) = i; }

///////////////////////
// uart
///////////////////////

struct uartlite_regs *const ttyUL0 = (struct uartlite_regs *)0x10000000;

#define SR_TX_FIFO_FULL (1<<3) /* transmit FIFO full */
#define SR_TX_FIFO_EMPTY (1<<2) /* transmit FIFO empty */
#define SR_RX_FIFO_VALID_DATA (1<<0) /* data in receive FIFO */
#define SR_RX_FIFO_FULL (1<<1) /* receive FIFO full */

#define ULITE_CONTROL_RST_TX 0x01
#define ULITE_CONTROL_RST_RX 0x02

void uart_put_c(char c) {
while (ttyUL0->status & SR_TX_FIFO_FULL);
ttyUL0->tx_fifo = c;
}

char uart_check_read() { // 1: data ready, 0: no data
return (ttyUL0->status & SR_RX_FIFO_VALID_DATA) != 0;
}

char uart_get_c() {
return ttyUL0->rx_fifo;
}

void get(char *s, int n) {
char debug[20] = "Enter get()";
print_s(debug);
int i = 0;
while (uart_check_read() != 1);
while (uart_check_read() && i < n - 1) {
print_s(debug);
char c = uart_get_c();
uart_put_c(c);
if (c == '\r' || c == '\n') {
break; // Break if carriage return or newline is encountered
}
*(s + i) = c;
i++;
}
s[i] = '\0';
}

int _write(int file, char* ptr, int len) {
int _write(int file, char *ptr, int len) {
int i = 0;
for (; i < len; i++) {
uart_put_c(ptr[i]);
t1_put_char(ptr[i]);
}
return i;
}
Expand All @@ -62,17 +27,17 @@ void _exit(int code) {
}

void print_s(const char *c) {
while (*c) {
uart_put_c(*c);
c ++;
}
while (*c) {
t1_put_char(*c);
c++;
}
}

///////////////////////
// allocation
///////////////////////

extern char* __heapbegin;
extern char *__heapbegin;
char *heap_top;

char *_sbrk(int nbytes) {
Expand All @@ -91,37 +56,22 @@ char *_sbrk(int nbytes) {
///////////////////////

// We don't support FS
int _isatty(int file) {
return -1;
}
int _isatty(int file) { return -1; }

// We don't support proc
int _kill(int pid, int sig) {
return -1;
}
int _kill(int pid, int sig) { return -1; }

// We don't support proc
int _getpid() {
return -1;
}
int _getpid() { return -1; }

// We don't support FS
int _fstat(int file, struct stat* st) {
return -1;
}
int _fstat(int file, struct stat *st) { return -1; }

// We don't support close
int _close(int file) {
return -1;
}
int _close(int file) { return -1; }

// We don't support lseek
int _lseek(int file, int ptr, int dir) {
return -1;
}
int _lseek(int file, int ptr, int dir) { return -1; }

// TODO: We can support read
int _read(int file, char* ptr, int len) {
return -1;
}

int _read(int file, char *ptr, int len) { return -1; }
10 changes: 1 addition & 9 deletions tests/emurt/emurt.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,5 @@

#include <stdint.h>

struct uartlite_regs {
volatile unsigned int rx_fifo;
volatile unsigned int tx_fifo;
volatile unsigned int status;
volatile unsigned int control;
};

void get(char *s, int n);
void print_s(const char *c);

void place_counter(int i);
31 changes: 31 additions & 0 deletions tests/emurt/tests/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{ linkerScript, t1main, makeBuilder, writeText }:
let
builder = makeBuilder { casePrefix = "emurt-test"; };
in
{
simple = builder {
caseName = "simple";

dontUnpack = true;
csrc = writeText "simple-emurt-test.c" ''
#include <emurt.h>

void test() {
place_counter(1);
print_s("Hello, World\n");
place_counter(2);
}
'';

buildPhase = ''
runHook preBuild

$CC -T${linkerScript} \
$csrc \
${t1main} \
-o $pname.elf

runHook postBuild
'';
};
}
Loading