📄 usb_new_pck_general.vhdl
字号:
-- function IsNrInList (List: integer_vector; Number: integer)
-- return boolean is
-- begin
-- for i in List'range loop
-- if (List(i) = Number) then
-- return TRUE;
-- end if;
-- end loop;
-- return FALSE;
-- end IsNrInList;
-- function GetIndexInList (List: integer_vector; Number: integer)
-- return integer is
-- begin
-- for i in List'range loop
-- if (List(i) = Number) then
-- return i;
-- end if;
-- end loop;
-- -- if it didn't return yet, the element is not in the list
-- -- and we give an error
-- assert TRUE
-- report "ERROR -- Function GetIndexInList: Number not in List"
-- severity ERROR;
-- end GetIndexInList;
-- MSB detection
function GetMsbPosition (Arg: unsigned)
return integer is
subtype S_ARG_RANGE is integer range Arg'range;
variable Result: S_ARG_RANGE;
begin
Result := Arg'right;
for index in Arg'range loop
if (Arg(index) = '1') then
Result := index;
exit;
end if;
end loop;
return Result;
end GetMsbPosition;
-- LSB detection
function GetLsbPosition (Arg: unsigned)
return integer is
subtype S_ARG_RANGE is integer range Arg'range;
variable Result: S_ARG_RANGE;
begin
Result := Arg'left;
for index in Arg'reverse_range loop
if (Arg(index) = '1') then
Result := index;
exit;
end if;
end loop;
return Result;
end GetLsbPosition;
function InsertWord (Target: unsigned;
InsWord: unsigned;
Position: natural;
RevIns: boolean := FALSE)
return unsigned is
constant TARGET_LENGTH: integer := Target'length;
constant INSERT_LENGTH: integer := InsWord'length;
variable Result: unsigned(Target'range);
begin
assert (INSERT_LENGTH + Position <= TARGET_LENGTH)
report "Invalid call to InsertWord (Position + inslength > target)"
severity note;
Result := Target;
if (RevIns) then
for i in (INSERT_LENGTH-1) downto 0 loop
Result(Position+i) := InsWord(InsWord'left - i);
end loop;
else
Result(INSERT_LENGTH + Position - 1 downto Position) := InsWord;
end if;
return Result;
end InsertWord;
function ExtractMSBits (Arg: unsigned; NrBits: integer)
return unsigned is
begin
assert (NrBits <= Arg'length)
report "Arg does not have NrBits bits"
severity ERROR;
assert (NrBits > 1)
report "NrBits should be larger than 1"
severity ERROR;
return Arg(Arg'left downto Arg'left-NrBits+1);
end ExtractMSBits;
function ExtractMSB (Arg: unsigned)
return one_bit is
begin
return Arg(Arg'left);
end ExtractMSB;
function ExtractLSBits (Arg: unsigned; NrBits: integer)
return unsigned is
begin
assert (NrBits <= Arg'length)
report "Arg does not have NrBits bits"
severity ERROR;
assert (NrBits > 1)
report "NrBits should be larger than 1"
severity ERROR;
return Arg(Arg'right+NrBits-1 downto Arg'right);
end ExtractLSBits;
function ExtractLSB (Arg: unsigned)
return one_bit is
begin
return Arg(Arg'right);
end ExtractLSB;
function AssembleLSBits (Arg: unsigned; Target: unsigned)
return unsigned is
variable Result: unsigned(Target'range);
begin
assert (Arg'length <= Target'length)
report "Argument too wide for target"
severity ERROR;
Result := (others => '0');
Result(Result'right+Arg'length-1 downto Result'right) := Arg;
return Result;
end AssembleLSBits;
function AssembleLSB (Arg: one_bit; Target: unsigned)
return unsigned is
variable Result: unsigned(Target'range);
begin
Result := (others => '0');
Result(Result'right) := Arg;
return Result;
end AssembleLSB;
function AssembleMSBits (Arg: unsigned; Target: unsigned)
return unsigned is
variable Result: unsigned(Target'range);
begin
assert (Arg'length <= Target'length)
report "Argument too wide for target"
severity ERROR;
Result := (others => '0');
Result(Result'left downto Result'left-Arg'length+1) := Arg;
return Result;
end AssembleMSBits;
-- Function IncMod: modulo increment of Arg
function IncMod (Arg: natural; Modulo: positive)
return natural is
begin
assert (Arg < Modulo)
report "Argument larger than Modulo (function IncMod)"
severity Error;
if (Arg = Modulo - 1) then
return 0;
else
return Arg + 1;
end if;
end IncMod;
-- Function DecMod: modulo decrement of Arg
function DecMod (Arg: natural; Modulo: positive)
return natural is
begin
assert (Arg < Modulo)
report "Argument larger than Modulo (function DecMod)"
severity Error;
if (Arg = 0) then
return Modulo - 1;
else
return Arg - 1;
end if;
end DecMod;
-- Function IncModRange: modulo increment of Arg in range LB..UB
function IncModRange (Arg: integer; LB: integer; UB: integer)
return integer is
begin
assert (Arg >= LB) and (Arg <= UB)
report "Argument not in range (function IncModRange)"
severity Error;
if (Arg = UB) then
return LB;
else
return Arg + 1;
end if;
end IncModRange;
-- Function DecModRange: modulo decrement of Arg in range LB..UB
function DecModRange (Arg: integer; LB: integer; UB: integer)
return integer is
begin
assert (Arg >= LB) and (Arg <= UB)
report "Argument not in range (function DecModRange)"
severity Error;
if (Arg = LB) then
return UB;
else
return Arg - 1;
end if;
end DecModRange;
-- Saturatable increment function
function IncSat (Arg: natural; SatVal: positive)
return natural is
begin
assert (Arg < SatVal)
report "Argument larger than Saturation Value (function IncSat)"
severity Error;
if (Arg = SatVal - 1) then
return Arg;
else
return Arg + 1;
end if;
end IncSat;
function IsAtMaxValue (Arg: natural; Modulo: positive)
return boolean is
begin
assert (Arg < Modulo)
report "Argument larger or equal to Modulo (function IsAtMaxValue)"
severity Error;
if (Arg = Modulo - 1) then
return TRUE;
else
return FALSE;
end if;
end IsAtMaxValue;
function Minimum (Arg1: integer; Arg2: integer)
return integer is
begin
if (Arg2 < Arg1) then
return Arg2;
else
return Arg1;
end if;
end Minimum;
function Maximum (Arg1: integer; Arg2: integer)
return integer is
begin
if (Arg2 > Arg1) then
return Arg2;
else
return Arg1;
end if;
end Maximum;
function Log2 (Arg: positive)
return natural is
begin
assert Arg <= 2**30 report "Arg too large for Log2 (greater than 2**30)";
for i in 1 to 30 loop
if Arg <= 2**i then
return i;
end if;
end loop;
end Log2;
function EventOccurred (Arg1: one_bit; Arg2: one_bit)
return boolean is
begin
return (Arg1 /= Arg2);
end EventOccurred;
function EventOccurred (Arg1: boolean; Arg2: boolean)
return boolean is
begin
return (Arg1 /= Arg2);
end EventOccurred;
function MakeEvent (Arg: one_bit)
return one_bit is
begin
return not Arg;
end MakeEvent;
function MakeEvent (Arg: boolean)
return boolean is
begin
return not Arg;
end MakeEvent;
function IsRisingEdge (Current: one_bit; Previous: one_bit)
return boolean is
begin
return ((Previous = LOW) and (Current = HIGH));
end IsRisingEdge;
function IsRisingEdge (Current: boolean; Previous: boolean)
return boolean is
begin
return ((Previous = FALSE) and (Current = TRUE));
end IsRisingEdge;
function IsFallingEdge (Current: one_bit; Previous: one_bit)
return boolean is
begin
return ((Previous = HIGH) and (Current = LOW));
end IsFallingEdge;
function IsFallingEdge (Current: boolean; Previous: boolean)
return boolean is
begin
return ((Previous = TRUE) and (Current = FALSE));
end IsFallingEdge;
function conv_active_low (Arg: boolean)
return one_bit is
begin
if (Arg) then
return one_bit' ('0');
else
return one_bit' ('1');
end if;
end conv_active_low;
function conv_active_high (Arg: boolean)
return one_bit is
begin
if (Arg) then
return one_bit' ('1');
else
return one_bit' ('0');
end if;
end conv_active_high;
function conv_bit (Arg: boolean)
return one_bit is
begin
if (Arg) then
return one_bit' ('1');
else
return one_bit' ('0');
end if;
end conv_bit;
function conv_active_low (Arg: one_bit)
return boolean is
begin
if (Arg = one_bit' ('0')) then
return TRUE;
else
return FALSE;
end if;
end conv_active_low;
function conv_active_high (Arg: one_bit)
return boolean is
begin
if (Arg = one_bit' ('1')) then
return TRUE;
else
return FALSE;
end if;
end conv_active_high;
function conv_active_low (Arg: booleans)
return unsigned is
variable Result: unsigned(Arg'range);
begin
for i in Result'range loop
Result(i) := conv_active_low(Arg(i));
end loop;
return Result;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -