OpenCores
URL https://opencores.org/ocsvn/openarty/openarty/trunk

Subversion Repositories openarty

[/] [openarty/] [trunk/] [sim/] [verilated/] [enetctrlsim.cpp] - Blame information for rev 58

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 58 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    enetctrlsim.cpp
4
//
5
// Project:     OpenArty, an entirely open SoC based upon the Arty platform
6
//
7
// Purpose:     
8
//
9
// Creator:     Dan Gisselquist, Ph.D.
10
//              Gisselquist Technology, LLC
11
//
12
////////////////////////////////////////////////////////////////////////////////
13
//
14
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
15
//
16
// This program is free software (firmware): you can redistribute it and/or
17
// modify it under the terms of  the GNU General Public License as published
18
// by the Free Software Foundation, either version 3 of the License, or (at
19
// your option) any later version.
20
//
21
// This program is distributed in the hope that it will be useful, but WITHOUT
22
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
23
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
24
// for more details.
25
//
26
// You should have received a copy of the GNU General Public License along
27
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
28
// target there if the PDF file isn't present.)  If not, see
29
// <http://www.gnu.org/licenses/> for a copy.
30
//
31
// License:     GPL, v3, as defined and found on www.gnu.org,
32
//              http://www.gnu.org/licenses/gpl.html
33
//
34
//
35
////////////////////////////////////////////////////////////////////////////////
36
//
37
//
38
#include <stdio.h>
39
#include <assert.h>
40
#include "enetctrlsim.h"
41
 
42
ENETCTRLSIM::ENETCTRLSIM(void) {
43
        m_consecutive_clocks = 0;
44
        m_synched = false;
45
        m_lastclk = 0;
46
        m_lastout = 0;
47
        m_tickcount = 0;
48
        m_ticks_per_clock = 0;
49
        m_halfword = 0;
50
        m_datareg = -1;
51
        PHY_ADDR = 1;
52
        TICKS_PER_CLOCK = 4;
53
        for(int i=0; i<ENET_MEMWORDS; i++)
54
                m_mem[i] = 0;
55
        m_outreg = -1;
56
}
57
 
58
int     ENETCTRLSIM::operator()(int in_reset, int clk, int data) {
59
        int     posedge, negedge, output = 1;
60
 
61
        posedge = ((clk)&&(!m_lastclk));
62
        negedge = ((!clk)&&(m_lastclk));
63
 
64
        m_tickcount++;
65
 
66
        if (in_reset) {
67
                m_consecutive_clocks = 0;
68
                m_synched = false;
69
                m_lastout = 1;
70
                m_datareg = -1;
71
 
72
                m_lastclk = clk;
73
 
74
                return 1;
75
        }
76
 
77
        if (posedge) {
78
                if ((data)&&(m_consecutive_clocks < 128))
79
                        m_consecutive_clocks++;
80
                else if (!data)
81
                        m_consecutive_clocks = 0;
82
                if ((m_tickcount != m_ticks_per_clock)
83
                        ||(m_ticks_per_clock < TICKS_PER_CLOCK)) {
84
                        m_consecutive_clocks = 0;
85
                        m_synched = false;
86
                } m_ticks_per_clock = m_tickcount;
87
                m_tickcount = 0;
88
        }
89
        if (m_consecutive_clocks > 32) {
90
                if (!m_synched)
91
                        printf("ENETCTRL: SYNCH!\n");
92
                m_synched = true;
93
                m_lastout = 1;
94
                m_halfword = 0;
95
                m_datareg = -1;
96
        }
97
 
98
        if ((posedge)&&(m_synched)) {
99
                m_datareg = (m_datareg<<1)|(data&1);
100
                if ((!m_halfword)&&((m_datareg&0x8000)==0)) {
101
                        printf("ENETCTRL::HALF-CMD: %08x\n", m_datareg);
102
                        m_halfword = 1;
103
                        int cmd = (m_datareg>>12)&0x0f;
104
                        int phy = (m_datareg>>7)&0x01f;
105
                        if ((cmd != 6)&&(cmd != 5))
106
                                printf("ENETCTRL: Unknown command, %d, expecting either 5 or 6\n", cmd);
107
                        if (phy != PHY_ADDR)
108
                                printf("ENETCTRL: Unknown PHY, %d, expecting %d\n", phy, PHY_ADDR);
109
                        if ((cmd == 6)&&(phy==PHY_ADDR)) {
110
                                int addr = (m_datareg>>2)&0x01f;
111
                                m_outreg = ((m_mem[addr]&0x0ffff)<<15)|0x080007fff;
112
                                printf("ENETCTRL: Sending %04x = MEM[%01x]\n",
113
                                        m_mem[addr]&0x0ffff, addr);
114
                        }
115
                } else if ((m_halfword)&&(m_halfword < 16)) {
116
                        m_halfword++;
117
                } else if (m_halfword) {
118
                        printf("ENETCTRL::FULL-CMD: %08x\n", m_datareg);
119
                        m_halfword = 0;
120
                        int cmd = (m_datareg>>28)&0x0f;
121
                        int phy = (m_datareg>>23)&0x01f;
122
                        if ((cmd != 6)&&(cmd != 5))
123
                                printf("ENETCTRL: Unknown command, %d, expecting either 5 or 6\n", cmd);
124
                        if (phy != PHY_ADDR)
125
                                printf("ENETCTRL: Unknown PHY, %d, expecting %d\n", phy, PHY_ADDR);
126
                        if ((cmd==5)&&(phy==PHY_ADDR)) {
127
                                int     addr;
128
 
129
                                if (m_datareg & 0x010000)
130
                                        printf("ERR: ENETCTRL, write command and bit 16 is active!\n");
131
                                assert((m_datareg & 0x010000)==0);
132
                                addr = (m_datareg>>18)&0x1f;
133
                                m_mem[addr] = m_datareg & 0x0ffff;
134
                                printf("ENETCTRL: Setting MEM[%01x] = %04x\n",
135
                                        addr, m_datareg&0x0ffff);
136
                        }
137
                        m_datareg = -1;
138
                }
139
        } else if (negedge) {
140
                m_outreg = (m_outreg<<1)|1;
141
        } output = (m_outreg&0x40000000)?1:0;
142
 
143
 
144
        m_lastclk = clk;
145
        return (data)&(output)&1;
146
}
147
 
148
int     ENETCTRLSIM::operator[](int index) const {
149
        return m_mem[index & (ENET_MEMWORDS-1)] & 0x0ffff;
150
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.