用于VHDL DFF代码的简单C ++包装器

时间:2013-04-30 15:46:20

标签: c++ interface-builder wrapper vhdl

我想知道,如何为简单的VHDL D-flip-flop代码编写C ++包装器。代码如下:

entity DFF is  
  Port ( D : in std_logic;  
       CLK : in std_logic := '0';  
       Q : out std_logic;  
       QN : out std_logic);  
 end DFF;  

 architecture behv of DFF is  
   begin  
      process (CLK)  
         begin  
             if CLK'event and CLK = '1' then  
                 Q <= D;  
                 QN <= NOT D;  
             end if;  
     end process;  
结束behv;

提前谢谢,
Milind。

2 个答案:

答案 0 :(得分:0)

你可以从这开始:

//Template Method pattern is used here to describe Port interface
class Port{
public:
    void behavior(){
         architecture_behavior();
    }
    void setD(bool input)(){
         doSetD(input);
    }
    void setCLK(bool clk){
         doSetCLK(clk);
    }
    bool state(){
        return doState();
    }
protected:
    virtual void architecture_behavior();
    virtual void doSetD(bool input);
    virtual void doSetCLK(bool clk);
    virtual void doState();
};

class Dflipflop : public Port{
public:
    Dflipflop(bool clk, bool d){
        CLK=clk;
        D=d;
    }
private:
    void architecture_behavior(){
        if(CLK=true){
            Q=D;
            QN=!Q;
        }
    }
    void doSetD(bool d){
        D=d;
    }
    void doSetCLK(bool clk){
        CLK=clk;
    }
    bool doState(){
        return Q;
    }
};

这可能更接近你想要的东西:

class Dflipflop2 : public Port{
public:
    Dflipflop2(bool clk, bool d){
        CLK=clk;
        D=d;
    }
    void architecture_behavior(bool d, bool clk){ // set state
        D=d;
        if(CLK=true && clk==false){ //set Q only if clock switches from 1 to 0
            Q=D;
            QN=!Q;
        }
        CLK=clk;
    }
 private:
    bool doState(){
        return Q;
    }
};

答案 1 :(得分:-1)

你可以试试这样的东西(注意:CLK改变每个5-ms的奇数组,并且根据调用函数的时间输出变化,所以不要看到实时CLK的表示。

#include <iostream>
#include <ctime>

#define PASSED_MS (int(float(now)/CLOCKS_PER_SEC*1000))
using namespace std;

class DFF
{
    public:
    DFF(bool D):D(D),CLK(0),Q(0),QN(1),start(clock()),now(start){}
    ~DFF();
    bool DFF_Q();
    void DFF_ALL(bool& clk,bool& q,bool& qn); //To return all Parameters at once

    private:
    bool D,CLK,Q,QN;
    const clock_t start;
    clock_t now;
};

DFF::~DFF()
{
  delete &D;
  delete &CLK;
  delete &Q;
  delete &QN;
  delete &start;
  delete &now;
}

bool DFF::DFF_Q()
{
    now=clock()-start;
    if((PASSED_MS/5)%2!=0 && PASSED_MS!=0) //if an odd period of 5ms passed change else keep it as its
    {
        CLK=CLK?0:1;
        if(CLK)
        {
            Q=D;
            QN=D?0:1;
        }
    }
     return Q;
}
void DFF::DFF_ALL(bool& clk,bool& q,bool& qn)
{
    DFF_Q();
    clk=CLK;
    q=Q;
    qn=QN;
}

int main()
{
    DFF a(1);
    bool clk,q,qn;
    int c=30;
    cout<<"CLK "<<"DFF-Q "<<"DFF-QN "<<endl;
    while(c!=0)
    {
        c--;
       a.DFF_ALL(clk,q,qn);
       cout<<clk<<"    "<<q<<"      "<<qn<<endl<<endl;
    }
    return 0;
}