Skip to content

Commit f79be54

Browse files
committed
Connect other HW modules
1 parent 4843711 commit f79be54

File tree

11 files changed

+518
-36
lines changed

11 files changed

+518
-36
lines changed

VirtualPrototype/inc/BusCtrl.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
#include "Log.h"
2424

2525
/**
26-
* Memory mapped Trace peripheral address
27-
*/
26+
* Memory mapped Trace peripheral address
27+
*/
2828
#define TRACE_MEMORY_ADDRESS 0x40000000
2929

3030
#define TIMER_MEMORY_ADDRESS_LO 0x40004000
@@ -72,10 +72,10 @@ class BusCtrl: sc_core::sc_module {
7272
tlm_utils::simple_initiator_socket<BusCtrl> timer_socket;
7373

7474
//Our module's sockets
75-
/**
76-
* @brief TLM initiator socket filter module
77-
*/
7875
tlm_utils::simple_initiator_socket<BusCtrl> filter_socket;
76+
tlm_utils::simple_initiator_socket<BusCtrl> sobel_edge_detector_socket;
77+
tlm_utils::simple_initiator_socket<BusCtrl> receiver_socket; //VGA AMS socket
78+
tlm_utils::simple_initiator_socket<BusCtrl> transmiter_socket; //Ethernet AMS Socket
7979

8080
/**
8181
* @brief constructor

VirtualPrototype/inc/Memory.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ class Memory: sc_core::sc_module {
6161
// *********************************************
6262
virtual unsigned int transport_dbg(tlm::tlm_generic_payload &trans);
6363

64+
//Backdoor access to memory
65+
void backdoor_write(unsigned char*&data, unsigned int data_length, sc_dt::uint64 address);
66+
void backdoor_read(unsigned char*&data, unsigned int data_length, sc_dt::uint64 address);
67+
6468
private:
6569

6670
/**

VirtualPrototype/inc/address_map.hpp

Lines changed: 68 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,73 @@
22
#define ADDRESS_MAP_HPP
33

44

5-
#define CUSTOM_MEMORY_OFFSET 0x40005000
6-
7-
#define FILTER_MEMORY_OFFSET 0x00000003
8-
#define FILTER_MEMORY_SIZE 36
9-
#define FILTER_MEMORY_ADDRESS_LO 0x40005003
10-
#define FILTER_MEMORY_ADDRESS_HI 0x40005027
11-
12-
// #define IMG_FILTER_KERNEL 0x00000003u
13-
// #define SOBEL_INPUT_0 0x00000027u
14-
// #define SOBEL_INPUT_1 0x0000002Fu
15-
// #define SOBEL_OUTPUT 0x00000030u
16-
// #define IMG_INPUT 0x00000034u
17-
// #define IMG_INPROCESS_A 0x000E1034u
18-
// #define IMG_INPROCESS_B 0x0012C034u
19-
// #define IMG_INPROCESS_C 0x001C2034u
20-
// #define IMG_COMPRESSED 0x00258034u
21-
22-
// #define IMG_INPUT_SZ 0x000E1000u
23-
// #define IMG_INPROCESS_A_SZ 0x0004B000u
24-
// #define IMG_INPROCESS_B_SZ 0x00096000u
25-
// #define IMG_INPROCESS_C_SZ 0x00096000u
26-
// #define IMG_COMPRESSED_SZ 0x0004B000u
27-
28-
// #define MEM_START IMG_INPUT
29-
// #define MEM_FINISH IMG_COMPRESSED + IMG_COMPRESSED_SZ
5+
#define CUSTOM_MEMORY_OFFSET 0x40005000u
6+
7+
//Filter Kernel
8+
#define IMG_FILTER_KERNEL_OFFSET 0x00000003u
9+
#define IMG_FILTER_KERNEL_SIZE 36
10+
#define IMG_FILTER_KERNEL_ADDRESS_LO CUSTOM_MEMORY_OFFSET + IMG_FILTER_KERNEL_OFFSET
11+
#define IMG_FILTER_KERNEL_ADDRESS_HI IMG_FILTER_KERNEL_ADDRESS_LO + IMG_FILTER_KERNEL_SIZE
12+
13+
//Filter Output
14+
#define IMG_FILTER_OUTPUT_OFFSET 0x00000027u
15+
#define IMG_FILTER_OUTPUT_SIZE 1
16+
#define IMG_FILTER_OUTPUT_ADDRESS_LO CUSTOM_MEMORY_OFFSET + IMG_FILTER_OUTPUT_OFFSET
17+
#define IMG_FILTER_OUTPUT_ADDRESS_HI IMG_FILTER_OUTPUT_ADDRESS_LO + IMG_FILTER_OUTPUT_SIZE
18+
19+
//Sobel Input 0
20+
#define SOBEL_INPUT_0_OFFSET 0x00000028u
21+
#define SOBEL_INPUT_0_SIZE 8
22+
#define SOBEL_INPUT_0_ADDRESS_LO CUSTOM_MEMORY_OFFSET + SOBEL_INPUT_0_OFFSET
23+
#define SOBEL_INPUT_0_ADDRESS_HI SOBEL_INPUT_0_ADDRESS_LO + SOBEL_INPUT_0_SIZE
24+
25+
//Sobel Input 1
26+
#define SOBEL_INPUT_1_OFFSET 0x00000030u
27+
#define SOBEL_INPUT_1_SIZE 1
28+
#define SOBEL_INPUT_1_ADDRESS_LO CUSTOM_MEMORY_OFFSET + SOBEL_INPUT_1_OFFSET
29+
#define SOBEL_INPUT_1_ADDRESS_HI SOBEL_INPUT_1_ADDRESS_LO + SOBEL_INPUT_1_SIZE
30+
31+
//Sobel Output 1
32+
#define SOBEL_OUTPUT_OFFSET 0x00000031u
33+
#define SOBEL_OUTPUT_SIZE 4
34+
#define SOBEL_OUTPUT_ADDRESS_LO CUSTOM_MEMORY_OFFSET + SOBEL_OUTPUT_OFFSET
35+
#define SOBEL_OUTPUT_ADDRESS_HI SOBEL_OUTPUT_ADDRESS_LO + SOBEL_OUTPUT_SIZE
36+
37+
//Img Input
38+
#define IMG_INPUT_OFFSET 0x00000035u
39+
#define IMG_INPUT_SIZE 921600
40+
#define IMG_INPUT_ADDRESS_LO CUSTOM_MEMORY_OFFSET + IMG_INPUT_OFFSET
41+
#define IMG_INPUT_ADDRESS_HI IMG_INPUT_ADDRESS_LO + IMG_INPUT_SIZE
42+
43+
//Img In Process A
44+
#define IMG_INPROCESS_A_OFFSET 0x0000E135u
45+
#define IMG_INPROCESS_A_SIZE 307200
46+
#define IMG_INPROCESS_A_ADDRESS_LO CUSTOM_MEMORY_OFFSET + IMG_INPROCESS_A_OFFSET
47+
#define IMG_INPROCESS_A_ADDRESS_HI IMG_INPROCESS_A_ADDRESS_LO + IMG_INPROCESS_A_SIZE
48+
49+
//Img In Process B
50+
#define IMG_INPROCESS_B_OFFSET 0x0012C035u
51+
#define IMG_INPROCESS_B_SIZE 614400
52+
#define IMG_INPROCESS_B_ADDRESS_LO CUSTOM_MEMORY_OFFSET + IMG_INPROCESS_B_OFFSET
53+
#define IMG_INPROCESS_B_ADDRESS_HI IMG_INPROCESS_B_ADDRESS_LO + IMG_INPROCESS_B_SIZE
54+
55+
//Img In Process C
56+
#define IMG_INPROCESS_C_OFFSET 0x001C2035u
57+
#define IMG_INPROCESS_C_SIZE 614400
58+
#define IMG_INPROCESS_C_ADDRESS_LO CUSTOM_MEMORY_OFFSET + IMG_INPROCESS_C_OFFSET
59+
#define IMG_INPROCESS_C_ADDRESS_HI IMG_INPROCESS_C_ADDRESS_LO + IMG_INPROCESS_C_SIZE
60+
61+
//Img Compressed
62+
#define IMG_COMPRESSED_OFFSET 0x00258035u
63+
#define IMG_COMPRESSED_SIZE 307200
64+
#define IMG_COMPRESSED_ADDRESS_LO CUSTOM_MEMORY_OFFSET + IMG_COMPRESSED_OFFSET
65+
#define IMG_COMPRESSED_ADDRESS_HI IMG_COMPRESSED_ADDRESS_LO + IMG_COMPRESSED_SIZE
66+
67+
//Img Compressed
68+
#define IMG_OUTPUT_OFFSET 0x002A3035u
69+
#define IMG_OUTPUT_SIZE 307200
70+
#define IMG_OUTPUT_ADDRESS_LO CUSTOM_MEMORY_OFFSET + IMG_OUTPUT_OFFSET
71+
#define IMG_OUTPUT_ADDRESS_HI IMG_OUTPUT_ADDRESS_LO + IMG_OUTPUT_SIZE
72+
3073

3174
#endif // ADDRESS_MAP_HPP

VirtualPrototype/inc/ips_filter_tlm.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct ips_filter_tlm : public Filter<IPS_IN_TYPE_TB, IPS_OUT_TYPE_TB, IPS_FILTE
2121

2222
SC_CTOR(ips_filter_tlm): Filter<IPS_IN_TYPE_TB, IPS_OUT_TYPE_TB, IPS_FILTER_KERNEL_SIZE>(Filter<IPS_IN_TYPE_TB, IPS_OUT_TYPE_TB, IPS_FILTER_KERNEL_SIZE>::name()), img_target(img_target::name()) {
2323

24-
set_mem_attributes(FILTER_MEMORY_ADDRESS_LO, FILTER_MEMORY_ADDRESS_LO);
24+
set_mem_attributes(IMG_FILTER_KERNEL_ADDRESS_LO, IMG_FILTER_KERNEL_ADDRESS_LO);
2525
}
2626

2727
//Override do_when_transaction functions
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#ifdef EDGE_DETECTOR_AT_EN
2+
#ifndef SOBEL_EDGE_DETECTOR_HPP
3+
#define SOBEL_EDGE_DETECTOR_HPP
4+
5+
#include <systemc.h>
6+
#include "address_map.hpp"
7+
8+
SC_MODULE(Edge_Detector)
9+
{
10+
11+
#ifndef USING_TLM_TB_EN
12+
sc_inout<sc_uint<64>> data;
13+
sc_in<sc_uint<24>> address;
14+
#else
15+
sc_uint<64> data;
16+
sc_uint<24> address;
17+
#endif // USING_TLM_TB_EN
18+
19+
const double delay_full_adder_1_bit = 0.361;
20+
const double delay_full_adder = delay_full_adder_1_bit * 16;
21+
22+
const double delay_multiplier = 9.82;
23+
24+
const sc_int<16> sobelGradientX[3][3] = {{-1, 0, 1},
25+
{-2, 0, 2},
26+
{-1, 0, 1}};
27+
const sc_int<16> sobelGradientY[3][3] = {{-1, -2, -1},
28+
{ 0, 0, 0},
29+
{ 1, 2, 1}};
30+
31+
sc_int<16> localWindow[3][3];
32+
33+
sc_int<16> resultSobelGradientX;
34+
sc_int<16> resultSobelGradientY;
35+
36+
sc_int<16> localMultX[3][3];
37+
sc_int<16> localMultY[3][3];
38+
39+
sc_event gotLocalWindow;
40+
41+
sc_event rd_t, wr_t;
42+
43+
sc_event mult_x, mult_y, sum_x, sum_y;
44+
45+
SC_CTOR(Edge_Detector)
46+
{
47+
SC_THREAD(wr);
48+
SC_THREAD(rd);
49+
SC_THREAD(compute_sobel_gradient_x);
50+
SC_THREAD(compute_sobel_gradient_y);
51+
SC_THREAD(perform_mult_gradient_x);
52+
SC_THREAD(perform_mult_gradient_y);
53+
SC_THREAD(perform_sum_gradient_x);
54+
SC_THREAD(perform_sum_gradient_y);
55+
}
56+
57+
virtual void write();
58+
59+
virtual void read();
60+
61+
void wr();
62+
63+
void rd();
64+
65+
void compute_sobel_gradient_x();
66+
67+
void compute_sobel_gradient_y();
68+
69+
void perform_mult_gradient_x();
70+
71+
void perform_mult_gradient_y();
72+
73+
void perform_sum_gradient_x();
74+
75+
void perform_sum_gradient_y();
76+
77+
};
78+
79+
#endif // SOBEL_EDGE_DETECTOR_HPP
80+
#endif // EDGE_DETECTOR_AT_EN

VirtualPrototype/src/BusCtrl.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ void BusCtrl::b_transport(tlm::tlm_generic_payload &trans,
3030

3131
sc_dt::uint64 adr = trans.get_address() / 4;
3232

33-
34-
3533
switch (adr) {
3634
case TIMER_MEMORY_ADDRESS_HI / 4:
3735
case TIMER_MEMORY_ADDRESS_LO / 4:
@@ -43,10 +41,29 @@ void BusCtrl::b_transport(tlm::tlm_generic_payload &trans,
4341
trace_socket->b_transport(trans, delay);
4442
break;
4543
[[likely]] default:
46-
if (FILTER_MEMORY_ADDRESS_LO / 4 <= adr && adr < FILTER_MEMORY_ADDRESS_HI / 4){
44+
if ((IMG_FILTER_KERNEL_ADDRESS_LO / 4 <= adr && adr < IMG_FILTER_KERNEL_ADDRESS_HI / 4) ||
45+
(IMG_FILTER_OUTPUT_ADDRESS_LO / 4 <= adr && adr < IMG_FILTER_OUTPUT_ADDRESS_HI / 4))
46+
{
4747
std::cout << "Writing/Reading to Filter!" << std::endl;
4848
filter_socket->b_transport(trans, delay);
4949
}
50+
else if ((SOBEL_INPUT_0_ADDRESS_LO / 4 <= adr && adr <= SOBEL_INPUT_0_ADDRESS_HI / 4) ||
51+
(SOBEL_INPUT_1_ADDRESS_LO / 4 <= adr && adr <= SOBEL_INPUT_1_ADDRESS_HI / 4) ||
52+
(SOBEL_OUTPUT_ADDRESS_LO / 4 <= adr && adr <= SOBEL_OUTPUT_ADDRESS_HI / 4))
53+
{
54+
std::cout << "Writing/Reading to Sobel!" << std::endl;
55+
sobel_edge_detector_socket->b_transport(trans, delay);
56+
}
57+
else if ((IMG_INPUT_ADDRESS_LO / 4 <= adr && adr <= IMG_INPUT_ADDRESS_HI / 4))
58+
{
59+
std::cout << "Writing/Reading to Sobel!" << std::endl;
60+
receiver_socket->b_transport(trans, delay);
61+
}
62+
else if ((IMG_OUTPUT_ADDRESS_LO / 4 <= adr && adr <= IMG_OUTPUT_ADDRESS_HI / 4))
63+
{
64+
std::cout << "Writing/Reading to Sobel!" << std::endl;
65+
transmiter_socket->b_transport(trans, delay);
66+
}
5067
else {
5168
memory_socket->b_transport(trans, delay);
5269
}

VirtualPrototype/src/Memory.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,20 @@ void Memory::readHexFile(std::string const& filename) {
207207
SC_REPORT_ERROR("Memory", "Open file error");
208208
}
209209
}
210+
211+
void Memory::backdoor_write(unsigned char*&data, unsigned int data_length, sc_dt::uint64 address)
212+
{
213+
memcpy((mem + address), data, data_length);
214+
for (int i = 0; i < 10; i++) {
215+
printf("Backdoor Writing: %0d\n", *(mem + address+i));
216+
}
217+
}
218+
219+
void Memory::backdoor_read(unsigned char*&data, unsigned int data_length, sc_dt::uint64 address)
220+
{
221+
data = new unsigned char[data_length];
222+
memcpy(data, (mem + address), data_length);
223+
for (int i = 0; i < 10; i++) {
224+
printf("Backdoor Reading: %0d\n", *(mem + address+i));
225+
}
226+
}

VirtualPrototype/src/Simulator.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,23 @@
1616
#include <unistd.h>
1717
#include <chrono>
1818

19+
#include "address_map.hpp"
1920
#include "CPU.h"
2021
#include "Memory.h"
2122
#include "BusCtrl.h"
2223
#include "Trace.h"
2324
#include "Timer.h"
2425
#include "Debug.h"
2526

27+
//For pre-loading images
28+
#define STB_IMAGE_IMPLEMENTATION
29+
#include "inc/stb_image.h"
30+
#define STB_IMAGE_WRITE_IMPLEMENTATION
31+
#include "inc/stb_image_write.h"
32+
2633
//Our modules
2734
#include "ips_filter_tlm.hpp"
35+
#include "sobel_edge_detector_tlm.cpp"
2836

2937
std::string filename;
3038
bool debug_session = false;
@@ -78,6 +86,8 @@ SC_MODULE(Simulator) {
7886
if (debug_session) {
7987
Debug debug(cpu, MainMemory);
8088
}
89+
90+
pre_load_memory();
8191
}
8292

8393
~Simulator() {
@@ -87,6 +97,35 @@ SC_MODULE(Simulator) {
8797
delete trace;
8898
delete timer;
8999
}
100+
101+
void pre_load_memory()
102+
{
103+
int width, height, channels, pixel_count;
104+
unsigned char *noisy_img, *noisy_img2;
105+
106+
noisy_img = stbi_load("inputs/car_noisy_image.png", &width, &height, &channels, 0);
107+
pixel_count = width * height * channels;
108+
printf("Pixel Count %0d, Width: %0d, Height: %0d \n", pixel_count, width, height);
109+
110+
noisy_img2 = new unsigned char [pixel_count];
111+
112+
MainMemory->backdoor_write(noisy_img, pixel_count, INPUT_IMG_MEMORY_LO);
113+
//stbi_write_png("outputs/car_filtered_image.png", width, height, channels, noisy_img, width*channels);
114+
}
115+
116+
void save_img_from_memory()
117+
{
118+
int width, height, channels, pixel_count;
119+
unsigned char *img_ptr;
120+
121+
channels = 1;
122+
width = 640;
123+
height = 452;
124+
pixel_count = width * height * channels;
125+
MainMemory->backdoor_read(img_ptr, pixel_count, OUTPUT_IMG_MEMORY_LO);
126+
127+
stbi_write_png("outputs/car_filtered_image.png", width, height, channels, img_ptr, width*channels);
128+
}
90129
};
91130

92131
Simulator *top;
@@ -179,6 +218,10 @@ int sc_main(int argc, char *argv[]) {
179218

180219
std::cout << "Total elapsed time: " << elapsed_seconds.count() << "s" << std::endl;
181220
std::cout << "Simulated " << int(std::round(instructions)) << " instr/sec" << std::endl;
221+
222+
//Generate output image
223+
top->save_img_from_memory();
224+
182225
std::cout << "Press Enter to finish" << std::endl;
183226
std::cin.ignore();
184227

VirtualPrototype/src/img_target.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ struct img_target: sc_module
7979
// Obliged to implement read and write commands
8080
if (cmd == tlm::TLM_READ_COMMAND) {
8181
this->do_when_read_transaction(ptr, len, adr);
82-
dbgmodprint("Read at address %0x, data: %0x", address_offset, *ptr);
82+
//dbgmodprint("Read at address %0x, data: %0x", address_offset, *ptr);
8383
}
8484
else if (cmd == tlm::TLM_WRITE_COMMAND) {
85-
dbgmodprint("Write at address %0x, data: %0x", address_offset, *ptr);
85+
//dbgmodprint("Write at address %0x, data: %0x", address_offset, *ptr);
8686
this->do_when_write_transaction(ptr, len, adr);
8787
}
8888

0 commit comments

Comments
 (0)