finish sim

This commit is contained in:
SikongJueluo
2024-05-16 21:39:15 +08:00
parent 5eebe6e922
commit d345fed4e7
10 changed files with 62 additions and 527 deletions

View File

@@ -44,6 +44,8 @@ VERILATOR_FLAGS += -Wall
VERILATOR_FLAGS += --trace
# Check SystemVerilog assertions
VERILATOR_FLAGS += --assert
# Enable multithreading
# VERILATOR_FLAGS += --threads 4
# Generate coverage analysis
# VERILATOR_FLAGS += --coverage
# Run Verilator in debug mode

View File

@@ -17,9 +17,12 @@
#include <fstream>
#include <iostream>
#define IM_WIDTH 1936
#define IM_HEIGHT 1088
#define IM_SIZE (IM_WIDTH * IM_HEIGHT)
#define IN_WIDTH 700
#define IN_HEIGHT 500
#define IN_SIZE (IN_WIDTH * IN_HEIGHT)
#define OUT_WIDTH 640
#define OUT_HEIGHT 480
#define OUT_SIZE (OUT_WIDTH * OUT_HEIGHT)
using namespace std;
using namespace sc_core;
@@ -37,11 +40,9 @@ SC_MODULE (TB_ISP) {
sc_in<bool> im_en;
sc_in<uint32_t> im_data;
// uint16_t image[IM_HEIGHT][IM_WIDTH];
// uint32_t out[IM_HEIGHT][IM_WIDTH];
// uint32_t out_head = 0;
unique_ptr<uint16_t[]> image = make_unique<uint16_t[]>(IM_SIZE);
unique_ptr<uint32_t[]> out = make_unique<uint32_t[]>(IM_SIZE);
sc_out<bool> is_done;
unique_ptr<uint16_t[]> image;
unique_ptr<uint32_t[]> out = make_unique<uint32_t[]>(OUT_SIZE);
SC_CTOR (TB_ISP) {
SC_CTHREAD(send_Data, clk.pos());
@@ -54,21 +55,21 @@ SC_MODULE (TB_ISP) {
uint16_t pos_x = 0, pos_y = 0;
while (true)
{
if (data_que.read() && pos_y < IM_HEIGHT - 2) {
if (data_que.read() && pos_y < IN_HEIGHT - 2) {
data_en.write(1);
printf("x=%3d, y=%3d, data=0x%04x\t", pos_x, pos_y, image[( pos_y + 0 ) * IM_WIDTH + pos_x]);
printf("x=%3d, y=%3d, data=0x%04x\t", pos_x, pos_y, image[( pos_y + 1 ) * IM_WIDTH + pos_x]);
printf("x=%3d, y=%3d, data=0x%04x\n", pos_x, pos_y, image[( pos_y + 2 ) * IM_WIDTH + pos_x]);
printf("x=%4d, y=%4d, data=0x%04x\t", pos_x, pos_y, image[( pos_y + 0 ) * IN_WIDTH + pos_x]);
printf("x=%4d, y=%4d, data=0x%04x\t", pos_x, pos_y, image[( pos_y + 1 ) * IN_WIDTH + pos_x]);
printf("x=%4d, y=%4d, data=0x%04x\n", pos_x, pos_y, image[( pos_y + 2 ) * IN_WIDTH + pos_x]);
data_out[0].write(image[( pos_y + 0 ) * IM_WIDTH + pos_x]);
data_out[1].write(image[( pos_y + 1 ) * IM_WIDTH + pos_x]);
data_out[2].write(image[( pos_y + 2 ) * IM_WIDTH + pos_x]);
data_out[0].write(image[( pos_y + 0 ) * IN_WIDTH + pos_x]);
data_out[1].write(image[( pos_y + 1 ) * IN_WIDTH + pos_x]);
data_out[2].write(image[( pos_y + 2 ) * IN_WIDTH + pos_x]);
wait(1);
data_en.write(0);
if (pos_x++ >= IM_WIDTH) {
if (pos_x++ >= IN_WIDTH) {
pos_x = 0;
pos_y++;
}
@@ -82,18 +83,31 @@ SC_MODULE (TB_ISP) {
}
void read_Data(void) {
is_done.write(0);
uint16_t pos_x = 0, pos_y = 0;
uint32_t last_data = 0;
uint16_t cnt = 0;
while (true)
{
if (im_en.read()) {
out[pos_y * IM_WIDTH + pos_x] = im_data.read();
out[pos_y * OUT_WIDTH + pos_x] = im_data.read();
if (pos_x++ >= IM_WIDTH) {
if (pos_x++ >= OUT_WIDTH) {
pos_x = 0;
pos_y++;
}
}
if (last_data == im_data.read()) {
cnt++;
if (cnt >= 100) {
is_done.write(1);
}
} else {
cnt = 0;
}
last_data = im_data.read();
wait();
}
}
@@ -104,7 +118,7 @@ int sc_main(int argc, char* argv[]) {
// Open image
ifstream in_image;
ofstream out_image;
in_image.open("../Demosaic/sim/transform/test.bin", ios::in | ios::binary);
in_image.open("./transform/test.bin", ios::in | ios::binary);
out_image.open("./out.bin", ios::out | ios::binary);
if (!in_image.is_open()) {
cout << "Open image fail" << endl;
@@ -114,18 +128,18 @@ int sc_main(int argc, char* argv[]) {
}
// Read image
// uint8_t buf[IM_SIZE * 2] = {0};
auto buf = make_unique<uint8_t[]>(2 * IM_SIZE);
// vector<vector<uint8_t>> buf(IM_HEIGHT, vector<uint8_t>(IM_WIDTH, 0));
in_image.read((char*)buf.get(), IM_SIZE * 2);
// uint8_t buf[IN_SIZE * 2] = {0};
auto buf = make_unique<uint8_t[]>(2 * IN_SIZE);
// vector<vector<uint8_t>> buf(IN_HEIGHT, vector<uint8_t>(IN_WIDTH, 0));
in_image.read((char*)buf.get(), IN_SIZE * 2);
in_image.close();
// Reshape data
// uint16_t image[IM_HEIGHT][IM_WIDTH] = {0};
auto image = make_unique<uint16_t[]>(IM_SIZE);
// uint16_t image[IN_HEIGHT][IN_WIDTH] = {0};
auto image = make_unique<uint16_t[]>(IN_SIZE);
uint32_t i = 0;
for (int y = 0; y < IM_HEIGHT; y++) {
for (int x = 0; x < IM_WIDTH; x++) {
image[y * IM_WIDTH + x] = (uint16_t)buf[i] + ((uint16_t)buf[i + 1] << 8);
for (int y = 0; y < IN_HEIGHT; y++) {
for (int x = 0; x < IN_WIDTH; x++) {
image[y * IN_WIDTH + x] = (uint16_t)buf[i] + ((uint16_t)buf[i + 1] << 8);
i += 2;
}
}
@@ -167,6 +181,8 @@ int sc_main(int argc, char* argv[]) {
sc_signal<bool> out_en;
sc_signal<uint32_t> data_out;
sc_signal<bool> flag_done;
// Construct the Verilated model, from inside Visp.h
// Using unique_ptr is similar to "Visp* isp = new Visp" then deleting at end
@@ -195,6 +211,7 @@ int sc_main(int argc, char* argv[]) {
tb_isp.im_clk(out_clk);
tb_isp.im_en(out_en);
tb_isp.im_data(data_out);
tb_isp.is_done(flag_done);
tb_isp.image = move(image);
// You must do one evaluation before enabling waves, in order to allow
@@ -226,6 +243,9 @@ int sc_main(int argc, char* argv[]) {
reset.write(0); // Deassert reset
}
if (flag_done.read())
break;
// Simulate 1ns
sc_start(1, SC_NS);
}
@@ -240,9 +260,10 @@ int sc_main(int argc, char* argv[]) {
}
// Save output image
for (int y = 0; y < IM_HEIGHT; y++)
for(int x = 0; x < IM_WIDTH; x++)
out_image.write((const char *)&tb_isp.out[y * IM_WIDTH + x], sizeof(tb_isp.out[0]));
cout << "Ready to save raw RGB image" << endl;
for (int y = 0; y < OUT_HEIGHT; y++)
for(int x = 0; x < OUT_WIDTH; x++)
out_image.write((const char *)&tb_isp.out[y * OUT_WIDTH + x], sizeof(tb_isp.out[0]));
out_image.close();
// Return good completion status

BIN
sim/transform/im.tif Normal file

Binary file not shown.

23
sim/transform/raw_cut.py Normal file
View File

@@ -0,0 +1,23 @@
import imageio
import numpy as np
cut_width = 700
cut_height = 500
if __name__ == '__main__':
# txt = open('./test.dat', 'w')
binfile = open('./test.bin', "wb")
image = imageio.imread_v2('./im.tif')
print(image.shape)
cut = image[0:cut_height, 0:cut_width]
print(cut.shape)
cut = np.array(cut, dtype=np.int16)
for data in list(cut.flatten()):
# txt.write('%02x\n%02x\n' % (data & 0x00ff, (data & 0xff00) >> 4))
binfile.write(data)
# txt.close()
binfile.close()
# imageio.imsave('./test.tif', cut)

View File

@@ -0,0 +1,10 @@
import imageio
import numpy as np
im_width = 512
im_height = 256
if __name__ == '__main__':
raw = np.fromfile('./out.bin', dtype=np.int16)
image = raw.reshape((im_height, im_width))
imageio.imsave("./out.png", image)