-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP extend json4vhdl with b16 encodings
- Loading branch information
1138-4EB
committed
Nov 27, 2019
1 parent
41ce155
commit 3e9a005
Showing
6 changed files
with
165 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# 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 | ||
|
||
from os.path import join, dirname | ||
from vunit import VUnit | ||
|
||
root = dirname(__file__) | ||
|
||
ui = VUnit.from_argv() | ||
|
||
lib = ui.library("vunit_lib") | ||
lib.add_source_files(join(root, "test", "*.vhd")) | ||
ui.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
-- 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 | ||
|
||
package encodings_pkg is | ||
|
||
function b16decode(str: string) return string; | ||
function b16encode(str: string) return string; | ||
|
||
end; | ||
|
||
package body encodings_pkg is | ||
|
||
function to_natural (c: character) return natural is | ||
constant offset_0 : natural := character'pos('0'); | ||
constant offset_U : natural := character'pos('A') - 10; | ||
constant offset_l : natural := character'pos('a') - 10; | ||
variable num: integer := -1; | ||
begin | ||
num := character'pos(c); | ||
case c is | ||
when '0' to '9' => return num - offset_0; | ||
when 'A' to 'F' => return num - offset_U; | ||
when 'a' to 'f' => return num - offset_l; | ||
when others => return -1; | ||
end case; | ||
end; | ||
|
||
function b16decode(str: string) return string is | ||
variable res: string (1 to str'length/2); | ||
begin | ||
for x in 1 to str'length/2 loop | ||
res(x) := character'val(to_natural(str(2*x-1)) *16 + to_natural(str(2*x))); | ||
end loop; | ||
return res; | ||
end; | ||
|
||
function to_character(num: natural) return character is | ||
begin | ||
if num<10 then | ||
return character'val(num+48); | ||
end if; | ||
return character'val(num+87); | ||
end; | ||
|
||
function b16encode(str: string) return string is | ||
variable res: string (1 to 2*str'length); | ||
variable num: natural; | ||
begin | ||
for x in 1 to str'length loop | ||
num := character'pos(str(x)); | ||
res(2*x-1 to 2*x) := to_character(num / 16) & to_character(num rem 16); | ||
end loop; | ||
return res; | ||
end function; | ||
|
||
end; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
-- 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 vunit_lib; | ||
use vunit_lib.run_pkg.all; | ||
use vunit_lib.check_pkg.all; | ||
use vunit_lib.encodings_pkg.all; | ||
|
||
entity tb_encodings is | ||
generic (runner_cfg : string); | ||
end; | ||
|
||
architecture a of tb_encodings is | ||
constant str: string := "[""test"",[true,false,18,null,""hello""],[9,8],3324.34,832432,""world""]"; | ||
constant enc: string := "5b2274657374222c5b747275652c66616c73652c31382c6e756c6c2c2268656c6c6f225d2c5b392c385d2c333332342e33342c3833323433322c22776f726c64225d"; | ||
begin | ||
main : process | ||
variable gstr: string(1 to str'length); | ||
variable genc: string(1 to enc'length); | ||
begin | ||
test_runner_setup(runner, runner_cfg); | ||
gstr := b16decode(enc); | ||
genc := b16encode(str); | ||
check_equal(gstr, str); | ||
check_equal(genc, enc); | ||
test_runner_cleanup(runner); | ||
end process; | ||
end; |