⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 SystemC可以由C语言直接开发硬件
💻 CPP
字号:
/************************************************************************
 * file name:		main.cpp 
 * description:		  
 *
 * modification history
 * --------------------
 * 2003-6-21 19:19:21, created by zhuwei
 */

/* includes----------------------------------------------------------- */
#include "systemc.h"

/* defines------------------------------------------------------------ */
/* typedefs----------------------------------------------------------- */
/* externs------------------------------------------------------------ */
/* globals------------------------------------------------------------ */
/* forward declarations----------------------------------------------- */

struct producer: sc_module
{
	sc_in<bool>	clk; 
	sc_out<bool>	rst_n;
	sc_out<int>	a, b, c;

	FILE	*fp;
	
	void reg_fun()
	{
		
		int	v_a, v_b, v_c;
		bool	v_wen;
		
		if(!rst_n.read())
		{
			a = 0;
			b = 0;
			c = 0;	
		}
		else
		{
			int	ret;
			
			ret = fscanf(fp, "%d %d %d %d", &v_a, &v_b, &v_c, &v_wen);
			if( ret == EOF) 
			{
				fclose(fp);
				sc_stop();
				return;
			}
			
			if(v_wen)
			{
				a = v_a;
				b = v_b;
				c = v_c;
			}
			else
			{
				a = 0;
				b = 0;
				c = 0;
			}
			
			cout << "a: " << v_a << "\tb: " << v_b << "\tc: " << v_c << "\twen: " << v_wen << endl;
		}

	}

	void initial()
	{
		rst_n = 1;
		wait(5, SC_NS);
		rst_n = 0;
		wait(5, SC_NS);
		rst_n = 1;
	}

	//void combo_fun();

	SC_CTOR(producer) 
	{
		//----------------------------------------
		char	tem;

		fp = fopen("data", "r");
		if(fp == NULL)
		{
			cout << "Can't open file: data";
			return;
		}
	
		tem = '-';
		while(tem!='\n') // 第一行为注释
		{
			fscanf(fp, "%c", &tem); 
			cout << tem;
		}
		//----------------------------------------

		SC_THREAD(initial);

		SC_METHOD(reg_fun);
		sensitive_pos << clk;
		sensitive_neg << rst_n;
		dont_initialize();

	}
};

struct consumer: sc_module
{
	sc_in<bool>	clk; 
	sc_in<bool>	rst_n;
	sc_in<int>	a, b, c;

	sc_signal<int>	count_a, count_b, count_c;
	
	void reg_fun()
	{
		if(!rst_n.read())
		{
			count_a = 0;
			count_b = 0;
			count_c = 0;
		}
		else
		{
			count_a = count_a + a;
			count_b = count_b + b;
			count_c = count_c + c;
		}
	}

	//void combo_fun();

	SC_CTOR(consumer) 
	{
		SC_METHOD(reg_fun);
		sensitive_pos << clk;
		sensitive_neg << rst_n;
		dont_initialize();

		//SC_METHOD(combo_fun);
	}
};

int sc_main(int argc, char *argv[])
{
	sc_clock		clk("clk", 10, SC_NS);
	sc_signal<bool>		rst_n;
	sc_signal<int>		a, b, c;

	producer	pro("pro");
	pro(clk, rst_n, a, b, c);

	consumer	csm("csm");
	csm(clk, rst_n, a, b, c);

	sc_trace_file *tf = sc_create_vcd_trace_file("read_file");

	sc_trace(tf, clk, "clk");
	sc_trace(tf, rst_n, "rst_n");
	sc_trace(tf, a, "a");
	sc_trace(tf, b, "b");
	sc_trace(tf, c, "c");

	sc_trace(tf, csm.count_a, "count_a");
	sc_trace(tf, csm.count_b, "count_b");
	sc_trace(tf, csm.count_c, "count_c");

	sc_start(500, SC_NS);

	sc_close_vcd_trace_file(tf);

	// --Make sure you have ModelSim installed.--
	system("vcd2wlf read_file.vcd read_file.wlf");
	system("vsim read_file.wlf");

	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -