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

📄 sync_fifo.h

📁 systemc源码
💻 H
字号:
/************************************************************************
 * file name:		sync_fifo.h
 * description:		synchronous fifo
 *
 * modification history
 * --------------------
 * 2003-7-7 16:30:28, created by zhuwei
 */ 

#ifndef _SYNC_FIFO_H
#define _SYNC_FIFO_H

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

/* defines------------------------------------------------------------ */
#define FIFO_DEPTH 25
#define BRAKE_DISTANCE 0

/* typedefs----------------------------------------------------------- */
template <class T>
class sync_fifo: public sc_module
{
public:
	sc_in<bool>			reset;
	sc_in<bool>			clk;
	sc_in<bool>			wen, ren;
	sc_in<T>			din;
	sc_out<T>			dout;
	sc_out<bool>		full, empty;
	
	sc_signal<int>		count;
	// internal variables
	int	head, tail;
	T	fifo_mem[FIFO_DEPTH];
	
	// process function
	
	// fifo out
	void fifo_out()
	{
		while(true)
		{
			wait();
			
			if(reset.read())
			{
				T	tem;
				memset(&tem, -1, sizeof(T));
				dout = tem;
			}
			else if(ren.read() && !empty.read())
			{
				dout = fifo_mem[tail];
				COUTL(sc_time_stamp() << " tail:" << tail);
			}
		}
	}
	
	// fifo in
	void fifo_in()
	{
		while(true)
		{
			wait();
			
			if(!reset.read() && wen.read() && !full.read())
			{
				fifo_mem[head] = din;
				COUTL("fifo in: " << din );
				COUTL(sc_time_stamp() << " head:" << head);
			}
		}
	}
	
	// update head
	void update_head()
	{
		while(true)
		{
			wait();

			if(head > FIFO_DEPTH) assert(0);
			
			if(reset.read())
			{
				head = 0;
			}
			else if(wen.read() && !full.read())
			{
				head++;
				if(head == FIFO_DEPTH) head = 0;
			}
		}
	}
	
	// update tail
	void update_tail()
	{
		while(true)
		{
			wait();

			if(tail > FIFO_DEPTH) assert(0);
			
			if(reset.read())
			{
				tail = 0;
			}
			else if(ren.read() && !empty.read())
			{
				tail++;
				if(tail == FIFO_DEPTH) tail = 0;
			}
		}
	}
	
	// update counter
	void update_counter()
	{
		while(true)
		{
			wait();

			if(count > FIFO_DEPTH) assert(0);
			
			if(reset.read() /*|| count.read() == FIFO_DEPTH*/) count = 0;
			else
			{
				if(!ren.read() && !wen.read()) count = count;
				if(!ren.read() && wen.read() && count.read() != FIFO_DEPTH) count = count.read() + 1;
				if(ren.read() && !wen.read()) count = count.read() -1;
				if(ren.read() && wen.read()) count = count;
			}
		}
	}
	
	// update empty
	void update_empty()
	{
		empty = (count.read() == 0);
	}
	
	// update full
	void update_full()
	{
		full = (count.read() == /*FIFO_DEPTH*/(FIFO_DEPTH - BRAKE_DISTANCE));
		COUTL("--full: " << full.read() << ":" << sc_time_stamp() );
	}
	
	// Constructor
	SC_CTOR(sync_fifo): count(0), head(0), tail(0) 
	{
		SC_THREAD(fifo_out);
		sensitive_pos << clk;
		
		SC_THREAD(fifo_in);
		sensitive_pos << clk;
		
		SC_THREAD(update_head);
		sensitive_pos << clk;
		
		SC_THREAD(update_tail);
		sensitive_pos << clk;
		
		SC_THREAD(update_counter);
		sensitive_pos << clk;
		
		SC_METHOD(update_empty);
		sensitive << count;
		
		SC_METHOD(update_full);
		sensitive << count;
	}
};

/* externs------------------------------------------------------------ */
/* globals------------------------------------------------------------ */
/* forward declarations----------------------------------------------- */

#endif /* _SYNC_FIFO_H */

⌨️ 快捷键说明

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