diff --git a/src/main/scala/example/Configs.scala b/src/main/scala/example/Configs.scala index e2e77fe..ae64aa8 100644 --- a/src/main/scala/example/Configs.scala +++ b/src/main/scala/example/Configs.scala @@ -33,6 +33,14 @@ class WithSimBlockDevice extends Config((site, here, up) => { } }) +class WithSimpleNIC extends Config((site, here, up) => { + case BuildTop => (p: Parameters) => { + val top = Module(LazyModule(new ExampleTopWithSimpleNIC()(p)).module) + top.connectNicLoopback() + top + } +}) + class BaseExampleConfig extends Config( new WithoutTLMonitors ++ new WithSerialAdapter ++ @@ -52,5 +60,8 @@ class SimBlockDeviceConfig extends Config( class BlockDeviceModelConfig extends Config( new WithBlockDevice ++ new WithBlockDeviceModel ++ new BaseExampleConfig) +class SimpleNICConfig extends Config( + new WithSimpleNIC ++ new BaseExampleConfig) + class WithTwoTrackers extends WithNBlockDeviceTrackers(2) class WithFourTrackers extends WithNBlockDeviceTrackers(4) diff --git a/src/main/scala/example/Top.scala b/src/main/scala/example/Top.scala index d9b484a..a530553 100644 --- a/src/main/scala/example/Top.scala +++ b/src/main/scala/example/Top.scala @@ -41,3 +41,12 @@ class ExampleTopWithBlockDevice(implicit p: Parameters) extends ExampleTop class ExampleTopWithBlockDeviceModule(l: ExampleTopWithBlockDevice) extends ExampleTopModule(l) with HasPeripheryBlockDeviceModuleImp + +class ExampleTopWithSimpleNIC(implicit p: Parameters) extends ExampleTop + with HasPeripherySimpleNIC { + override lazy val module = new ExampleTopWithSimpleNICModule(this) +} + +class ExampleTopWithSimpleNICModule(outer: ExampleTopWithSimpleNIC) + extends ExampleTopModule(outer) + with HasPeripherySimpleNICModuleImp diff --git a/testchipip b/testchipip index 99eb48a..af9ce25 160000 --- a/testchipip +++ b/testchipip @@ -1 +1 @@ -Subproject commit 99eb48a02b161c05bef6fdc4eb07f7040bae9390 +Subproject commit af9ce25b097abc807693905314173a80513715c4 diff --git a/tests/Makefile b/tests/Makefile index edabb26..97c0f1f 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -3,7 +3,7 @@ OBJDUMP=riscv64-unknown-elf-objdump CFLAGS=-mcmodel=medany -std=gnu99 -O2 -fno-common -fno-builtin-printf -Wall LDFLAGS=-static -nostdlib -nostartfiles -lgcc -PROGRAMS = pwm blkdev accum charcount +PROGRAMS = pwm blkdev accum charcount nic-loopback default: $(addsuffix .riscv,$(PROGRAMS)) diff --git a/tests/nic-loopback.c b/tests/nic-loopback.c new file mode 100644 index 0000000..a2922d5 --- /dev/null +++ b/tests/nic-loopback.c @@ -0,0 +1,60 @@ +#include "mmio.h" +#include +#include + +#define SIMPLENIC_BASE 0x10016000L +#define SIMPLENIC_SEND (SIMPLENIC_BASE + 0) +#define SIMPLENIC_RECV (SIMPLENIC_BASE + 8) + +uint64_t src[200]; +uint64_t dst[201]; + +void nic_send(void *data, unsigned long len) +{ + uintptr_t addr = ((uintptr_t) data) & ((1L << 48) - 1); + unsigned long packet = (len << 48) | addr; + + asm volatile ("fence"); + reg_write64(SIMPLENIC_SEND, packet); +} + +void nic_recv(void *dest) +{ + uintptr_t addr = (uintptr_t) dest; + volatile uint64_t *words = (volatile uint64_t *) dest; + + reg_write64(SIMPLENIC_RECV, addr); + + // Poll for completion + while (!words[200]); + asm volatile ("fence"); +} + +#define TEST_LEN 128 + +int main(void) +{ + int i; + + for (i = 0; i < TEST_LEN; i++) + src[i] = i << 12; + + printf("Sending data on loopback NIC\n"); + + nic_send(src, TEST_LEN * sizeof(uint64_t)); + nic_recv(dst); + + printf("Received data\n"); + + for (i = 0; i < TEST_LEN; i++) { + if (dst[i] != src[i]) { + printf("Data mismatch @ %d: %lx != %lx\n", + i, dst[i], src[i]); + return 1; + } + } + + printf("All correct\n"); + + return 0; +}