2022-11-28 15:53:55 +00:00
|
|
|
// DESCRIPTION: Verilator: built-in packages and classes
|
|
|
|
//
|
|
|
|
// Code available from: https://verilator.org
|
|
|
|
//
|
|
|
|
//*************************************************************************
|
|
|
|
//
|
2024-01-01 08:19:59 +00:00
|
|
|
// Copyright 2022-2024 by Wilson Snyder. This program is free software; you can
|
2022-11-28 15:53:55 +00:00
|
|
|
// redistribute it and/or modify it under the terms of either the GNU Lesser
|
|
|
|
// General Public License Version 3 or the Perl Artistic License Version 2.0.
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0
|
|
|
|
//
|
|
|
|
//*************************************************************************
|
2022-12-03 16:31:26 +00:00
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
/// \brief Verilated IEEE std:: header
|
|
|
|
///
|
2024-11-13 03:11:19 +00:00
|
|
|
/// This file is included automatically by Verilator, unless '--no-std-package'
|
|
|
|
/// is used.
|
2022-12-03 16:31:26 +00:00
|
|
|
///
|
|
|
|
/// This file is not part of the Verilated public-facing API.
|
|
|
|
/// It is only for internal use.
|
|
|
|
///
|
|
|
|
//*************************************************************************
|
2024-12-01 16:35:00 +00:00
|
|
|
//
|
|
|
|
// The following keywords from this file are hardcoded for detection in the parser:
|
|
|
|
// "mailbox", "process", "randomize", "semaphore", "std"
|
2022-11-28 15:53:55 +00:00
|
|
|
|
|
|
|
// verilator lint_off DECLFILENAME
|
|
|
|
// verilator lint_off TIMESCALEMOD
|
|
|
|
// verilator lint_off UNUSEDSIGNAL
|
|
|
|
package std;
|
2023-04-07 01:27:16 +00:00
|
|
|
class mailbox #(type T);
|
|
|
|
protected int m_bound;
|
|
|
|
protected T m_queue[$];
|
2022-11-28 15:53:55 +00:00
|
|
|
|
2023-04-07 01:27:16 +00:00
|
|
|
function new(int bound = 0);
|
|
|
|
m_bound = bound;
|
|
|
|
endfunction
|
2022-11-28 15:53:55 +00:00
|
|
|
|
2023-04-07 01:27:16 +00:00
|
|
|
function int num();
|
|
|
|
return m_queue.size();
|
|
|
|
endfunction
|
2022-11-28 15:53:55 +00:00
|
|
|
|
2023-04-07 01:27:16 +00:00
|
|
|
task put(T message);
|
|
|
|
`ifdef VERILATOR_TIMING
|
2024-07-03 21:29:32 +00:00
|
|
|
while (m_bound != 0 && m_queue.size() >= m_bound)
|
2023-06-01 14:02:08 +00:00
|
|
|
wait (m_queue.size() < m_bound);
|
2023-04-07 01:27:16 +00:00
|
|
|
m_queue.push_back(message);
|
|
|
|
`endif
|
|
|
|
endtask
|
|
|
|
|
|
|
|
function int try_put(T message);
|
2023-10-23 12:33:22 +00:00
|
|
|
if (m_bound == 0 || num() < m_bound) begin
|
2022-11-28 15:53:55 +00:00
|
|
|
m_queue.push_back(message);
|
2023-04-07 01:27:16 +00:00
|
|
|
return 1;
|
|
|
|
end
|
|
|
|
return 0;
|
|
|
|
endfunction
|
2022-11-28 15:53:55 +00:00
|
|
|
|
2023-04-07 01:27:16 +00:00
|
|
|
task get(ref T message);
|
|
|
|
`ifdef VERILATOR_TIMING
|
2024-07-03 21:29:32 +00:00
|
|
|
while (m_queue.size() == 0) begin
|
|
|
|
wait (m_queue.size() > 0);
|
|
|
|
end
|
2023-04-07 01:27:16 +00:00
|
|
|
message = m_queue.pop_front();
|
|
|
|
`endif
|
|
|
|
endtask
|
2022-11-28 15:53:55 +00:00
|
|
|
|
2023-04-07 01:27:16 +00:00
|
|
|
function int try_get(ref T message);
|
|
|
|
if (num() > 0) begin
|
2022-11-28 15:53:55 +00:00
|
|
|
message = m_queue.pop_front();
|
2023-04-07 01:27:16 +00:00
|
|
|
return 1;
|
|
|
|
end
|
|
|
|
return 0;
|
|
|
|
endfunction
|
2022-11-28 15:53:55 +00:00
|
|
|
|
2023-04-07 01:27:16 +00:00
|
|
|
task peek(ref T message);
|
|
|
|
`ifdef VERILATOR_TIMING
|
2024-07-03 21:29:32 +00:00
|
|
|
while (m_queue.size() == 0) begin
|
|
|
|
wait (m_queue.size() > 0);
|
|
|
|
end
|
2023-04-07 01:27:16 +00:00
|
|
|
message = m_queue[0];
|
|
|
|
`endif
|
|
|
|
endtask
|
2022-11-28 15:53:55 +00:00
|
|
|
|
2023-04-07 01:27:16 +00:00
|
|
|
function int try_peek(ref T message);
|
|
|
|
if (num() > 0) begin
|
2022-11-28 15:53:55 +00:00
|
|
|
message = m_queue[0];
|
2023-04-07 01:27:16 +00:00
|
|
|
return 1;
|
|
|
|
end
|
|
|
|
return 0;
|
|
|
|
endfunction
|
|
|
|
endclass
|
2022-11-28 15:53:55 +00:00
|
|
|
|
2023-04-07 01:27:16 +00:00
|
|
|
class semaphore;
|
|
|
|
protected int m_keyCount;
|
2022-11-28 15:53:55 +00:00
|
|
|
|
2023-04-07 01:27:16 +00:00
|
|
|
function new(int keyCount = 0);
|
|
|
|
m_keyCount = keyCount;
|
|
|
|
endfunction
|
2022-11-28 15:53:55 +00:00
|
|
|
|
2023-04-07 01:27:16 +00:00
|
|
|
function void put(int keyCount = 1);
|
|
|
|
m_keyCount += keyCount;
|
|
|
|
endfunction
|
2022-11-28 15:53:55 +00:00
|
|
|
|
2023-04-07 01:27:16 +00:00
|
|
|
task get(int keyCount = 1);
|
|
|
|
`ifdef VERILATOR_TIMING
|
2024-07-03 21:29:32 +00:00
|
|
|
while (m_keyCount < keyCount) begin
|
|
|
|
wait (m_keyCount >= keyCount);
|
|
|
|
end
|
2023-04-07 01:27:16 +00:00
|
|
|
m_keyCount -= keyCount;
|
|
|
|
`endif
|
|
|
|
endtask
|
2022-11-28 15:53:55 +00:00
|
|
|
|
2023-04-07 01:27:16 +00:00
|
|
|
function int try_get(int keyCount = 1);
|
|
|
|
if (m_keyCount >= keyCount) begin
|
2022-11-28 15:53:55 +00:00
|
|
|
m_keyCount -= keyCount;
|
2023-04-07 01:27:16 +00:00
|
|
|
return 1;
|
|
|
|
end
|
|
|
|
return 0;
|
|
|
|
endfunction
|
|
|
|
endclass
|
2023-04-08 19:04:42 +00:00
|
|
|
|
|
|
|
class process;
|
2023-06-01 14:02:08 +00:00
|
|
|
typedef enum {
|
|
|
|
FINISHED = 0,
|
|
|
|
RUNNING = 1,
|
|
|
|
WAITING = 2,
|
|
|
|
SUSPENDED = 3,
|
|
|
|
KILLED = 4
|
|
|
|
} state;
|
|
|
|
|
|
|
|
`ifdef VERILATOR_TIMING
|
|
|
|
// Width visitor changes it to VlProcessRef
|
|
|
|
protected chandle m_process;
|
|
|
|
`endif
|
|
|
|
|
2023-04-08 19:04:42 +00:00
|
|
|
static function process self();
|
2023-06-01 14:02:08 +00:00
|
|
|
process p = new;
|
|
|
|
`ifdef VERILATOR_TIMING
|
|
|
|
$c(p.m_process, " = vlProcess;");
|
|
|
|
`endif
|
|
|
|
return p;
|
2023-04-08 19:04:42 +00:00
|
|
|
endfunction
|
2023-06-01 14:02:08 +00:00
|
|
|
|
|
|
|
protected function void set_status(state s);
|
|
|
|
`ifdef VERILATOR_TIMING
|
|
|
|
$c(m_process, "->state(", s, ");");
|
|
|
|
`endif
|
|
|
|
endfunction
|
|
|
|
|
2023-04-08 19:04:42 +00:00
|
|
|
function state status();
|
2023-06-01 14:02:08 +00:00
|
|
|
`ifdef VERILATOR_TIMING
|
|
|
|
return state'($c(m_process, "->state()"));
|
|
|
|
`else
|
2023-04-08 19:04:42 +00:00
|
|
|
return RUNNING;
|
2023-06-01 14:02:08 +00:00
|
|
|
`endif
|
2023-04-08 19:04:42 +00:00
|
|
|
endfunction
|
2023-06-01 14:02:08 +00:00
|
|
|
|
2023-04-08 19:04:42 +00:00
|
|
|
function void kill();
|
2023-06-01 14:02:08 +00:00
|
|
|
set_status(KILLED);
|
2023-04-08 19:04:42 +00:00
|
|
|
endfunction
|
2023-06-01 14:02:08 +00:00
|
|
|
|
2023-04-08 19:04:42 +00:00
|
|
|
function void suspend();
|
|
|
|
$error("std::process::suspend() not supported");
|
|
|
|
endfunction
|
2023-06-01 14:02:08 +00:00
|
|
|
|
2023-04-08 19:04:42 +00:00
|
|
|
function void resume();
|
2023-06-01 14:02:08 +00:00
|
|
|
set_status(RUNNING);
|
2023-04-08 19:04:42 +00:00
|
|
|
endfunction
|
2023-06-01 14:02:08 +00:00
|
|
|
|
|
|
|
task await();
|
|
|
|
`ifdef VERILATOR_TIMING
|
|
|
|
wait (status() == FINISHED || status() == KILLED);
|
|
|
|
`endif
|
|
|
|
endtask
|
|
|
|
|
|
|
|
// When really implemented, srandom must operate on the process, but for
|
2023-04-08 19:04:42 +00:00
|
|
|
// now rely on the srandom() that is automatically generated for all
|
|
|
|
// classes.
|
2023-06-01 14:02:08 +00:00
|
|
|
//
|
2023-04-08 19:04:42 +00:00
|
|
|
// function void srandom(int seed);
|
|
|
|
// endfunction
|
2023-06-01 14:02:08 +00:00
|
|
|
|
|
|
|
// The methods below work only if set_randstate is never applied to
|
|
|
|
// a state string created before another such string. Full support
|
|
|
|
// could use VlRNG class to store the state per process in VlProcess
|
|
|
|
// objects.
|
2023-04-08 19:04:42 +00:00
|
|
|
function string get_randstate();
|
2023-06-01 14:02:08 +00:00
|
|
|
string s;
|
|
|
|
|
|
|
|
s.itoa($random); // Get a random number
|
|
|
|
set_randstate(s); // Pretend it's the state of RNG
|
|
|
|
return s;
|
2023-04-08 19:04:42 +00:00
|
|
|
endfunction
|
2023-06-01 14:02:08 +00:00
|
|
|
|
|
|
|
function void set_randstate(string s);
|
|
|
|
$urandom(s.atoi()); // Set the seed using a string
|
2023-04-08 19:04:42 +00:00
|
|
|
endfunction
|
|
|
|
endclass
|
2024-08-09 21:21:32 +00:00
|
|
|
function int randomize();
|
|
|
|
randomize = 0;
|
|
|
|
endfunction
|
2022-11-28 15:53:55 +00:00
|
|
|
endpackage
|