-
Notifications
You must be signed in to change notification settings - Fork 297
Expand file tree
/
Copy pathpkg_c.vhd
More file actions
81 lines (60 loc) · 2.6 KB
/
Copy pathpkg_c.vhd
File metadata and controls
81 lines (60 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
-- This Source Code Form is subject to the terms of the Mozilla Public
-- License, v. 2.0. If a copy of the MPL was not distributed with this file,
-- You can obtain one at http://mozilla.org/MPL/2.0/.
--
-- Copyright (c) 2014-2019, Lars Asplund lars.anders.asplund@gmail.com
library ieee;
context ieee.ieee_std_context;
package pkg_c is
function get_param(f: integer) return integer;
attribute foreign of get_param : function is "VHPIDIRECT get_param";
procedure write_byte ( id, i, v: integer ) ;
attribute foreign of write_byte : procedure is "VHPIDIRECT write_byte";
impure function read_byte ( id, i: integer ) return integer;
attribute foreign of read_byte : function is "VHPIDIRECT read_byte";
type memory_t is record
-- Private
p_id: integer;
end record;
impure function new_memory(id: integer := -1) return memory_t;
procedure write_word ( memory: memory_t; i: natural; w: std_logic_vector );
impure function read_word ( memory: memory_t; i, bytes_per_word: integer ) return std_logic_vector;
procedure write_byte ( memory: memory_t; i: integer; v: std_logic_vector(7 downto 0) );
impure function read_byte ( memory: memory_t; i: integer ) return std_logic_vector;
end pkg_c;
package body pkg_c is
-- VHPI
function get_param(f: integer) return integer is begin
assert false report "VHPI" severity failure;
end function;
procedure write_byte ( id, i, v: integer ) is begin
assert false report "VHPI" severity failure;
end procedure;
impure function read_byte ( id, i: integer ) return integer is begin
assert false report "VHPI" severity failure;
end function;
-- VHDL
procedure write_byte ( memory: memory_t; i: integer; v: std_logic_vector(7 downto 0) ) is
begin
write_byte(memory.p_id, i, to_integer(unsigned(v)));
end procedure;
impure function read_byte ( memory: memory_t; i: integer ) return std_logic_vector is begin
return std_logic_vector(to_unsigned(read_byte(memory.p_id, i), 8));
end function;
impure function new_memory(id: integer := -1) return memory_t is begin
return (p_id => id);
end;
procedure write_word ( memory: memory_t; i: natural; w: std_logic_vector ) is begin
for idx in 0 to w'length/8-1 loop
write_byte(memory, i + idx, w(8*idx+7 downto 8*idx));
end loop;
end procedure;
impure function read_word ( memory: memory_t; i, bytes_per_word: integer ) return std_logic_vector is
variable tmp: std_logic_vector(31 downto 0);
begin
for idx in 0 to bytes_per_word-1 loop
tmp(8*idx+7 downto 8*idx) := read_byte(memory, i + idx);
end loop;
return tmp;
end function;
end pkg_c;