Can't get to_integer to work

P

Panic

I have an array that is supposed to act as a ROM-
lookup-table (actually a Rijndael G-box), and so I
want to give it a byte input and get a byte output.

The byte input, let's call it ADDR, is std_logic_vector(7 downto 0).
Sinse the indices of my "ROM"-array are integers, I need to convert
the ADDR vector to type integer. My "ROM"-array has 256 elements,
and the lookup should work something like this:

sub_byte <= ROM( 16*to_integer(ADDR(7 downto 4)) + to_integer(ADDR(3 downto
0)));

My problem is, no matter how I try, I can't get to_integer to work. I've
tried looking
at the FAQ, and I have search the net, and I have tried what has been
suggested, but
I can't seem to get it to work. So I need your help to convert ADDR from
being
a std_logic_vector(7 downto 0) into an integer.

Here is (part of) my code:

subbyte <= g_rom(addr) after lookup_delay;
type srd_array is array (0 to 255) of std_logic_vector(7 downto 0);
constant g_rom : srd_array := (

x"00",x"01",x"8D",x"F6",x"CB",x"52",x"7B",x"D1",x"E8",x"4F",x"29",x"C0",x"B0
",x"E1",x"E5",x"C7",

x"74",x"B4",x"AA",x"4B",x"99",x"2B",x"60",x"5F",x"58",x"3F",x"FD",x"CC",x"FF
",x"40",x"EE",x"B2",

x"3A",x"6E",x"5A",x"F1",x"55",x"4D",x"A8",x"C9",x"C1",x"0A",x"98",x"15",x"30
",x"44",x"A2",x"C2",

x"2C",x"45",x"92",x"6C",x"F3",x"39",x"66",x"42",x"F2",x"35",x"20",x"6F",x"77
",x"BB",x"59",x"19",

x"1D",x"FE",x"37",x"67",x"2D",x"31",x"F5",x"69",x"A7",x"64",x"AB",x"13",x"54
",x"25",x"E9",x"09",

x"ED",x"5C",x"05",x"CA",x"4C",x"24",x"87",x"BF",x"18",x"3E",x"22",x"F0",x"51
",x"EC",x"61",x"17",

x"16",x"5E",x"AF",x"D3",x"49",x"A6",x"36",x"43",x"F4",x"47",x"91",x"DF",x"33
",x"93",x"21",x"3B",

x"79",x"B7",x"97",x"85",x"10",x"B5",x"BA",x"3C",x"B6",x"70",x"D0",x"06",x"A1
",x"FA",x"81",x"82",

x"83",x"7E",x"7F",x"80",x"96",x"73",x"BE",x"56",x"9B",x"9E",x"95",x"D9",x"F7
",x"02",x"B9",x"A4",

x"DE",x"6A",x"32",x"6D",x"D8",x"8A",x"84",x"72",x"2A",x"14",x"9F",x"88",x"F9
",x"DC",x"89",x"9A",

x"FB",x"7C",x"2E",x"C3",x"8F",x"B8",x"65",x"48",x"26",x"C8",x"12",x"4A",x"CE
",x"E7",x"D2",x"62",

x"0C",x"E0",x"1F",x"EF",x"11",x"75",x"78",x"71",x"A5",x"8E",x"76",x"3D",x"BD
",x"BC",x"86",x"57",

x"0B",x"28",x"2F",x"A3",x"DA",x"D4",x"E4",x"0F",x"A9",x"27",x"53",x"04",x"1B
",x"FC",x"AC",x"E6",

x"7A",x"07",x"AE",x"63",x"C5",x"DB",x"E2",x"EA",x"94",x"8B",x"C4",x"D5",x"9D
",x"F8",x"90",x"6B",

x"B1",x"0D",x"D6",x"EB",x"C6",x"0E",x"CF",x"AD",x"08",x"4E",x"D7",x"E3",x"5D
",x"50",x"1E",x"B3",

x"5B",x"23",x"38",x"34",x"68",x"46",x"03",x"8C",x"DD",x"9C",x"7D",x"A0",x"CD
",x"1A",x"41",x"1C"
);
 
T

Tim Hubberstey

Panic said:
I have an array that is supposed to act as a ROM-
lookup-table (actually a Rijndael G-box), and so I
want to give it a byte input and get a byte output.

The byte input, let's call it ADDR, is std_logic_vector(7 downto 0).
Sinse the indices of my "ROM"-array are integers, I need to convert
the ADDR vector to type integer. My "ROM"-array has 256 elements,
and the lookup should work something like this:

sub_byte <= ROM( 16*to_integer(ADDR(7 downto 4)) + to_integer(ADDR(3 downto
0)));

You can't convert std_logic_vector directly to integer. If you check the
function prototype, you'll find that to_integer only accepts signed or
unsigned as arguments. Try:

sub_byte <= ROM( 16*to_integer(unsigned(ADDR(7 downto 4))) +
to_integer(unsigned(ADDR(3 downto 0))));

I'm also not sure why you think you need to operate on slices of ADDR.
This should also work as long as ADDR is 31 bits or less:

sub_byte <= ROM(to_integer(unsigned(ADDR)));
 
P

Panic

sub_byte <= ROM( 16*to_integer(unsigned(ADDR(7 downto 4))) +
to_integer(unsigned(ADDR(3 downto 0))));

Tried thatone before. Didn't work. That what's baffles me, since I thought I
had understood how to_integer and unsigned works....

The error I get (for both unsigned and to_integer) is something about
element type mismatch.
I'm also not sure why you think you need to operate on slices of ADDR.
This should also work as long as ADDR is 31 bits or less:

ADDR is a byte, but the lookup is actually a matrix lookup. That's why I
need to do the calculation.
 
I

Ian Poole

What libraries are you using? Can you post some more code???

--
Ian Poole, Consultant

DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * Perl * Tcl/Tk * Verification * Project Services

Doulos Ltd. Church Hatch, 22 Market Place, Ringwood, Hampshire, BH24 1AW, UK
Tel: +44 (0)1425 471223 mail: (e-mail address removed)
Fax: +44 (0)1425 471573 Web: http://www.doulos.com

The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.
 
J

Jim Lewis

What Tim said works if you are using package numeric_std:

use ieee.numeric_std.all ;


if instead you are using, std_logic_arith then use
conv_integer instead.

For new designs, you should be using numeric_std.

Cheers,
Jim

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jim Lewis
Director of Training mailto:[email protected]
SynthWorks Design Inc. http://www.SynthWorks.com
1-503-590-4787

Expert VHDL Training for Hardware Design and Verification
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
T

Tim Hubberstey

Panic said:
ADDR is a byte, but the lookup is actually a matrix lookup. That's why I
need to do the calculation.

You may think so but what you wrote (complete with errors):

sub_byte <= ROM( 16*to_integer(ADDR(7 downto 4)) + to_integer(ADDR(3
downto 0)));

is EXACTLY equivalent to:

sub_byte <= ROM(to_integer(unsigned(ADDR)));

Just check the arithmetic if you don't believe me. X*16 and X shifted
left by 4 are identical. You may have intended something else but what
you say doesn't jive with what you wrote.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top