2017-09-18 07:30:54 +02:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2018-09-29 20:01:11 +02:00
|
|
|
// Copyright (C) 2017, MINRES Technologies GmbH
|
|
|
|
// All rights reserved.
|
2017-09-25 22:00:46 +02:00
|
|
|
//
|
2018-09-29 20:01:11 +02:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// 3. Neither the name of the copyright holder nor the names of its contributors
|
|
|
|
// may be used to endorse or promote products derived from this software
|
|
|
|
// without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
|
|
|
// Contributors:
|
|
|
|
// eyck@minres.com - initial implementation
|
2017-09-25 22:00:46 +02:00
|
|
|
//
|
|
|
|
//
|
2017-09-18 07:30:54 +02:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "gpio.h"
|
2018-09-29 20:01:11 +02:00
|
|
|
#include "scc/report.h"
|
2017-10-04 15:16:52 +02:00
|
|
|
#include "scc/utilities.h"
|
2018-09-29 20:01:11 +02:00
|
|
|
#include "gen/gpio_regs.h"
|
|
|
|
#include <limits>
|
2017-09-18 07:30:54 +02:00
|
|
|
|
|
|
|
namespace sysc {
|
|
|
|
|
2018-11-04 13:43:58 +01:00
|
|
|
using namespace sc_core;
|
|
|
|
|
|
|
|
gpio::gpio(sc_module_name nm)
|
|
|
|
: sc_module(nm)
|
2017-09-19 17:02:23 +02:00
|
|
|
, tlm_target<>(clk)
|
2017-09-18 07:30:54 +02:00
|
|
|
, NAMED(clk_i)
|
2017-09-19 17:37:29 +02:00
|
|
|
, NAMED(rst_i)
|
2018-09-29 20:01:11 +02:00
|
|
|
, NAMED(pins_o, 32)
|
|
|
|
, NAMED(pins_i, 32)
|
|
|
|
, NAMED(iof0_o, 32)
|
|
|
|
, NAMED(iof1_o, 32)
|
|
|
|
, NAMED(iof0_i, 32)
|
|
|
|
, NAMED(iof1_i, 32)
|
2018-11-04 13:43:58 +01:00
|
|
|
, NAMEDD(regs, gpio_regs)
|
2018-09-29 20:01:11 +02:00
|
|
|
{
|
2017-09-19 17:02:23 +02:00
|
|
|
regs->registerResources(*this);
|
2017-09-18 07:30:54 +02:00
|
|
|
SC_METHOD(clock_cb);
|
2017-09-25 22:00:46 +02:00
|
|
|
sensitive << clk_i;
|
2017-09-19 17:02:23 +02:00
|
|
|
SC_METHOD(reset_cb);
|
2017-09-25 22:00:46 +02:00
|
|
|
sensitive << rst_i;
|
2018-09-29 20:01:11 +02:00
|
|
|
dont_initialize();
|
2021-06-01 18:08:32 +02:00
|
|
|
auto pins_i_cb =[this](unsigned int tag, tlm::scc::tlm_signal_gp<>& gp,
|
2018-11-04 13:43:58 +01:00
|
|
|
tlm::tlm_phase& phase, sc_time& delay)->tlm::tlm_sync_enum{
|
2018-09-29 20:01:11 +02:00
|
|
|
this->pin_input(tag, gp, delay);
|
|
|
|
return tlm::TLM_COMPLETED;
|
|
|
|
};
|
|
|
|
auto i=0U;
|
|
|
|
for(auto& s:pins_i){
|
|
|
|
s.register_nb_transport(pins_i_cb, i);
|
|
|
|
++i;
|
|
|
|
}
|
2021-06-01 18:08:32 +02:00
|
|
|
auto iof0_i_cb =[this](unsigned int tag, tlm::scc::tlm_signal_gp<>& gp,
|
2018-11-04 13:43:58 +01:00
|
|
|
tlm::tlm_phase& phase, sc_time& delay)->tlm::tlm_sync_enum{
|
2018-09-29 20:01:11 +02:00
|
|
|
last_iof0[tag]=gp.get_value();
|
|
|
|
this->iof_input(tag, 0, gp, delay);
|
|
|
|
return tlm::TLM_COMPLETED;
|
|
|
|
};
|
|
|
|
i=0;
|
|
|
|
for(auto& s:iof0_i){
|
|
|
|
s.register_nb_transport(iof0_i_cb, i);
|
|
|
|
++i;
|
|
|
|
}
|
2021-06-01 18:08:32 +02:00
|
|
|
auto iof1_i_cb =[this](unsigned int tag, tlm::scc::tlm_signal_gp<>& gp,
|
2018-11-04 13:43:58 +01:00
|
|
|
tlm::tlm_phase& phase, sc_time& delay)->tlm::tlm_sync_enum{
|
2018-09-29 20:01:11 +02:00
|
|
|
last_iof1[tag]=gp.get_value();
|
|
|
|
this->iof_input(tag, 1, gp, delay);
|
|
|
|
return tlm::TLM_COMPLETED;
|
|
|
|
};
|
|
|
|
i=0;
|
|
|
|
for(auto& s:iof1_i){
|
|
|
|
s.register_nb_transport(iof1_i_cb, i);
|
|
|
|
++i;
|
|
|
|
}
|
2018-11-04 13:43:58 +01:00
|
|
|
auto update_pins_cb = [this](scc::sc_register<uint32_t> ®, uint32_t data, sc_time d) -> bool {
|
2018-09-29 20:01:11 +02:00
|
|
|
if (!this->regs->in_reset()) {
|
|
|
|
auto changed_bits = (reg.get()^data);
|
|
|
|
reg.put(data);
|
|
|
|
update_pins(changed_bits);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
regs->port.set_write_cb(update_pins_cb);
|
|
|
|
regs->output_en.set_write_cb(update_pins_cb);
|
|
|
|
regs->out_xor.set_write_cb(update_pins_cb);
|
|
|
|
regs->iof_en.set_write_cb(update_pins_cb);
|
|
|
|
regs->iof_sel.set_write_cb(update_pins_cb);
|
2017-09-18 07:30:54 +02:00
|
|
|
}
|
|
|
|
|
2017-09-25 22:00:46 +02:00
|
|
|
gpio::~gpio() {}
|
2017-09-18 07:30:54 +02:00
|
|
|
|
2017-09-19 17:02:23 +02:00
|
|
|
void gpio::reset_cb() {
|
2018-09-29 20:01:11 +02:00
|
|
|
if (rst_i.read()){
|
2017-09-19 17:02:23 +02:00
|
|
|
regs->reset_start();
|
2018-09-29 20:01:11 +02:00
|
|
|
} else {
|
2017-09-19 17:02:23 +02:00
|
|
|
regs->reset_stop();
|
2018-09-29 20:01:11 +02:00
|
|
|
}
|
|
|
|
update_pins(std::numeric_limits<uint32_t>::max());
|
|
|
|
}
|
|
|
|
|
|
|
|
void gpio::clock_cb() {
|
|
|
|
this->clk = clk_i.read();
|
|
|
|
}
|
|
|
|
|
2021-06-01 18:08:32 +02:00
|
|
|
tlm::tlm_phase gpio::write_output(tlm::scc::tlm_signal_gp<bool>& gp, size_t i, bool val) {
|
2018-11-04 13:43:58 +01:00
|
|
|
sc_time delay{SC_ZERO_TIME};
|
2018-09-29 20:01:11 +02:00
|
|
|
tlm::tlm_phase phase{ tlm::BEGIN_REQ };
|
|
|
|
gp.set_command(tlm::TLM_WRITE_COMMAND);
|
|
|
|
gp.set_response_status(tlm::TLM_OK_RESPONSE);
|
|
|
|
gp.set_value(val);
|
|
|
|
pins_o.at(i)->nb_transport_fw(gp, phase, delay);
|
|
|
|
return phase;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gpio::update_pins(uint32_t changed_bits) {
|
2018-11-04 13:43:58 +01:00
|
|
|
sc_inout_rv<32>::data_type out_val;
|
2021-06-01 18:08:32 +02:00
|
|
|
tlm::scc::tlm_signal_gp<bool> gp;
|
2018-09-29 20:01:11 +02:00
|
|
|
bool val;
|
|
|
|
for(size_t i=0, mask = 1; i<32; ++i, mask<<=1){
|
|
|
|
if(changed_bits&mask){
|
|
|
|
if((regs->r_iof_en&mask!=0) && (iof0_i[i].size()==0 || iof1_i[i].size()==0)){
|
|
|
|
if((regs->r_iof_sel&mask)==0 && iof0_i[i].size()>0){
|
|
|
|
val=last_iof0[i]?sc_dt::Log_1:sc_dt::Log_0;
|
|
|
|
} else if((regs->r_iof_sel&mask)==1 && iof1_i[i].size()>0)
|
|
|
|
val=last_iof1[i]?sc_dt::Log_1:sc_dt::Log_0;
|
|
|
|
} else {
|
|
|
|
if((regs->r_output_en&mask) && (regs->r_port&mask))
|
|
|
|
val=true;
|
|
|
|
else
|
|
|
|
val=false;
|
|
|
|
if(regs->r_out_xor&mask)
|
|
|
|
val=~val;
|
|
|
|
}
|
|
|
|
tlm::tlm_phase phase = write_output(gp, i, val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-01 18:08:32 +02:00
|
|
|
void gpio::pin_input(unsigned int tag, tlm::scc::tlm_signal_gp<bool>& gp, sc_time& delay) {
|
2018-09-29 20:01:11 +02:00
|
|
|
if(delay>SC_ZERO_TIME){
|
|
|
|
wait(delay);
|
|
|
|
delay=SC_ZERO_TIME;
|
|
|
|
}
|
|
|
|
auto mask = 1u<<tag;
|
|
|
|
switch(gp.get_value()){
|
|
|
|
case true:
|
|
|
|
if(regs->r_output_en&mask==0)
|
|
|
|
regs->r_value|=mask;
|
|
|
|
forward_pin_input(tag, gp);
|
|
|
|
break;
|
|
|
|
case false:
|
|
|
|
if(regs->r_output_en&mask==0) regs->r_value&=~mask;
|
|
|
|
forward_pin_input(tag, gp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-01 18:08:32 +02:00
|
|
|
void gpio::forward_pin_input(unsigned int tag, tlm::scc::tlm_signal_gp<bool>& gp) {
|
2018-09-29 20:01:11 +02:00
|
|
|
const auto mask = 1U<<tag;
|
|
|
|
if(regs->iof_en&mask){
|
|
|
|
auto& socket = regs->iof_sel&mask?iof1_o[tag]:iof0_o[tag];
|
2021-06-01 18:08:32 +02:00
|
|
|
tlm::scc::tlm_signal_gp<> new_gp;
|
2018-09-29 20:01:11 +02:00
|
|
|
for(size_t i=0; i<socket.size(); ++i){
|
2018-11-04 13:43:58 +01:00
|
|
|
sc_time delay{SC_ZERO_TIME};
|
2018-09-29 20:01:11 +02:00
|
|
|
tlm::tlm_phase phase{tlm::BEGIN_REQ};
|
|
|
|
new_gp.set_command(tlm::TLM_WRITE_COMMAND);
|
|
|
|
new_gp.set_response_status(tlm::TLM_OK_RESPONSE);
|
|
|
|
new_gp.set_value(gp.get_value());
|
|
|
|
new_gp.update_extensions_from(gp);
|
|
|
|
socket->nb_transport_fw(new_gp, phase, delay); // we don't care about phase and sync enum
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-01 18:08:32 +02:00
|
|
|
void gpio::iof_input(unsigned int tag, unsigned iof_idx, tlm::scc::tlm_signal_gp<>& gp, sc_time& delay) {
|
2018-09-29 20:01:11 +02:00
|
|
|
if(delay>SC_ZERO_TIME){
|
|
|
|
wait(delay);
|
|
|
|
delay=SC_ZERO_TIME;
|
|
|
|
}
|
|
|
|
const auto mask = 1U<<tag;
|
|
|
|
if(regs->r_iof_en&mask){
|
|
|
|
const auto idx = regs->r_iof_sel&mask?1:0;
|
|
|
|
if(iof_idx == idx){
|
|
|
|
auto& socket = pins_o[tag];
|
|
|
|
for(size_t i=0; i<socket.size(); ++i){
|
2018-11-04 13:43:58 +01:00
|
|
|
sc_time delay{SC_ZERO_TIME};
|
2018-09-29 20:01:11 +02:00
|
|
|
tlm::tlm_phase phase{tlm::BEGIN_REQ};
|
2021-06-01 18:08:32 +02:00
|
|
|
tlm::scc::tlm_signal_gp<> new_gp;
|
2018-09-29 20:01:11 +02:00
|
|
|
new_gp.set_command(tlm::TLM_WRITE_COMMAND);
|
|
|
|
auto val = gp.get_value();
|
|
|
|
new_gp.set_value(val);
|
|
|
|
new_gp.copy_extensions_from(gp);
|
|
|
|
socket->nb_transport_fw(new_gp, phase, delay); // we don't care about phase and sync enum
|
|
|
|
gp.update_extensions_from(new_gp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-18 07:30:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace sysc */
|
2018-09-29 20:01:11 +02:00
|
|
|
|