2017-10-04 10:31:11 +02:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Copyright 2017 eyck@minres.com
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
|
// use this file except in compliance with the License. You may obtain a copy
|
|
|
|
// of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing permissions and limitations under
|
|
|
|
// the License.
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "sysc/SiFive/clint.h"
|
2017-10-04 14:30:25 +02:00
|
|
|
|
|
|
|
#include "scc/utilities.h"
|
2017-10-04 10:31:11 +02:00
|
|
|
#include "sysc/SiFive/gen/clint_regs.h"
|
|
|
|
|
|
|
|
namespace sysc {
|
|
|
|
|
|
|
|
const int lfclk_mutiplier = 1 << 12;
|
|
|
|
|
|
|
|
clint::clint(sc_core::sc_module_name nm)
|
|
|
|
: sc_core::sc_module(nm)
|
|
|
|
, tlm_target<>(clk)
|
|
|
|
, NAMED(clk_i)
|
|
|
|
, NAMED(rst_i)
|
|
|
|
, NAMED(mtime_int_o)
|
|
|
|
, NAMED(msip_int_o)
|
|
|
|
, NAMEDD(clint_regs, regs)
|
|
|
|
, cnt_fraction(0) {
|
|
|
|
regs->registerResources(*this);
|
|
|
|
SC_METHOD(clock_cb);
|
|
|
|
sensitive << clk_i;
|
|
|
|
SC_METHOD(reset_cb);
|
|
|
|
sensitive << rst_i;
|
|
|
|
dont_initialize();
|
2017-10-04 14:30:25 +02:00
|
|
|
regs->mtimecmp.set_write_cb([this](scc::sc_register<uint64_t> ®, uint64_t data) -> bool {
|
2017-10-04 10:31:11 +02:00
|
|
|
if (!regs->in_reset()) {
|
|
|
|
reg.put(data);
|
|
|
|
this->update_mtime();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
2017-10-04 14:30:25 +02:00
|
|
|
regs->mtime.set_read_cb([this](const scc::sc_register<uint64_t> ®, uint64_t &data) -> bool {
|
2017-10-04 10:31:11 +02:00
|
|
|
this->update_mtime();
|
|
|
|
data = reg.get();
|
|
|
|
return true;
|
|
|
|
});
|
2017-10-04 14:30:25 +02:00
|
|
|
regs->mtime.set_write_cb([this](scc::sc_register<uint64_t> ®, uint64_t data) -> bool { return false; });
|
|
|
|
regs->msip.set_write_cb([this](scc::sc_register<uint32_t> ®, uint32_t data) -> bool {
|
2017-10-04 10:31:11 +02:00
|
|
|
reg.put(data);
|
|
|
|
msip_int_o.write(regs->r_msip.msip);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
SC_METHOD(update_mtime);
|
|
|
|
sensitive << mtime_evt;
|
|
|
|
dont_initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void clint::clock_cb() {
|
|
|
|
update_mtime();
|
|
|
|
clk = clk_i.read();
|
|
|
|
update_mtime();
|
|
|
|
}
|
|
|
|
|
|
|
|
clint::~clint() {}
|
|
|
|
|
|
|
|
void clint::reset_cb() {
|
|
|
|
if (rst_i.read()) {
|
|
|
|
regs->reset_start();
|
|
|
|
msip_int_o.write(false);
|
|
|
|
mtime_int_o.write(false);
|
|
|
|
cnt_fraction = 0;
|
|
|
|
} else
|
|
|
|
regs->reset_stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void clint::update_mtime() {
|
2017-10-05 14:53:52 +02:00
|
|
|
auto diff = (sc_core::sc_time_stamp() - last_updt) / clk;
|
2017-10-04 10:31:11 +02:00
|
|
|
auto diffi = (int)diff;
|
|
|
|
regs->r_mtime += (diffi + cnt_fraction) / lfclk_mutiplier;
|
|
|
|
cnt_fraction = (cnt_fraction + diffi) % lfclk_mutiplier;
|
|
|
|
mtime_evt.cancel();
|
2017-10-05 14:53:52 +02:00
|
|
|
if (regs->r_mtimecmp > regs->r_mtime && clk > sc_core::SC_ZERO_TIME) {
|
2017-10-04 10:31:11 +02:00
|
|
|
sc_core::sc_time next_trigger = (clk * lfclk_mutiplier) * (regs->r_mtimecmp - regs->mtime) - cnt_fraction * clk;
|
|
|
|
mtime_evt.notify(next_trigger);
|
|
|
|
} else
|
|
|
|
mtime_int_o.write(true);
|
2017-10-05 14:53:52 +02:00
|
|
|
last_updt = sc_core::sc_time_stamp();
|
2017-10-04 10:31:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace sysc */
|