Verilog - Can you `define a bit slice?
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
Can you define
a bit slice in Verilog?
For example, is this possible:
`define opcode 5:3 // is this possible?
reg [ 7:0 ] a, b;
...
if ( a[ `opcode ] == someValue ) // a[5:3]
doStuff
if ( b[ `opcode ] == someValue ) // b[5:3]
doStuff
fpga verilog
add a comment |Â
up vote
2
down vote
favorite
Can you define
a bit slice in Verilog?
For example, is this possible:
`define opcode 5:3 // is this possible?
reg [ 7:0 ] a, b;
...
if ( a[ `opcode ] == someValue ) // a[5:3]
doStuff
if ( b[ `opcode ] == someValue ) // b[5:3]
doStuff
fpga verilog
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Can you define
a bit slice in Verilog?
For example, is this possible:
`define opcode 5:3 // is this possible?
reg [ 7:0 ] a, b;
...
if ( a[ `opcode ] == someValue ) // a[5:3]
doStuff
if ( b[ `opcode ] == someValue ) // b[5:3]
doStuff
fpga verilog
Can you define
a bit slice in Verilog?
For example, is this possible:
`define opcode 5:3 // is this possible?
reg [ 7:0 ] a, b;
...
if ( a[ `opcode ] == someValue ) // a[5:3]
doStuff
if ( b[ `opcode ] == someValue ) // b[5:3]
doStuff
fpga verilog
fpga verilog
edited 5 hours ago
asked 7 hours ago


Jet Blue
459314
459314
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
You can `define
almost any text you want. You would have to use a[`opcode]
with the backtick.
SystemVerilog gives you some other options.
The let
construct declares a name for an expression.
let opcode = a[5:3];
...
if (opcode==someValue)
You can use a packed struct.
struct packed
logic [1:0] field1;
logic [2:0] opcode;
logic [2:0] field2;
a;
Now you can refer to a.opcode
as the same as a[5:3]
.
shouldn't it belogic [5:3] opcode;
? - Ah wait, no, it's packed. of course. I presume that order of definition matters. - Is there another kind of struct for doing what I just said? some absolute addressing?
– Harry Svensson
5 hours ago
Ah oops, added the backticks
– Jet Blue
5 hours ago
add a comment |Â
up vote
0
down vote
Yes, you can.
But it's probably cleaner and easier to simply break the register into fields using wires:
wire [2:0] a_opcode = a[5:3];
if (a_opcode == someValue) ...
How many different registers are you going to be extracting opcodes from?
Note that this solution only works for reading a_opcode, not writing to it. Most of the time, that is OK.
– dave_59
6 hours ago
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
You can `define
almost any text you want. You would have to use a[`opcode]
with the backtick.
SystemVerilog gives you some other options.
The let
construct declares a name for an expression.
let opcode = a[5:3];
...
if (opcode==someValue)
You can use a packed struct.
struct packed
logic [1:0] field1;
logic [2:0] opcode;
logic [2:0] field2;
a;
Now you can refer to a.opcode
as the same as a[5:3]
.
shouldn't it belogic [5:3] opcode;
? - Ah wait, no, it's packed. of course. I presume that order of definition matters. - Is there another kind of struct for doing what I just said? some absolute addressing?
– Harry Svensson
5 hours ago
Ah oops, added the backticks
– Jet Blue
5 hours ago
add a comment |Â
up vote
3
down vote
accepted
You can `define
almost any text you want. You would have to use a[`opcode]
with the backtick.
SystemVerilog gives you some other options.
The let
construct declares a name for an expression.
let opcode = a[5:3];
...
if (opcode==someValue)
You can use a packed struct.
struct packed
logic [1:0] field1;
logic [2:0] opcode;
logic [2:0] field2;
a;
Now you can refer to a.opcode
as the same as a[5:3]
.
shouldn't it belogic [5:3] opcode;
? - Ah wait, no, it's packed. of course. I presume that order of definition matters. - Is there another kind of struct for doing what I just said? some absolute addressing?
– Harry Svensson
5 hours ago
Ah oops, added the backticks
– Jet Blue
5 hours ago
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
You can `define
almost any text you want. You would have to use a[`opcode]
with the backtick.
SystemVerilog gives you some other options.
The let
construct declares a name for an expression.
let opcode = a[5:3];
...
if (opcode==someValue)
You can use a packed struct.
struct packed
logic [1:0] field1;
logic [2:0] opcode;
logic [2:0] field2;
a;
Now you can refer to a.opcode
as the same as a[5:3]
.
You can `define
almost any text you want. You would have to use a[`opcode]
with the backtick.
SystemVerilog gives you some other options.
The let
construct declares a name for an expression.
let opcode = a[5:3];
...
if (opcode==someValue)
You can use a packed struct.
struct packed
logic [1:0] field1;
logic [2:0] opcode;
logic [2:0] field2;
a;
Now you can refer to a.opcode
as the same as a[5:3]
.
edited 21 mins ago
Dave Tweed♦
109k9132236
109k9132236
answered 6 hours ago
dave_59
1,598614
1,598614
shouldn't it belogic [5:3] opcode;
? - Ah wait, no, it's packed. of course. I presume that order of definition matters. - Is there another kind of struct for doing what I just said? some absolute addressing?
– Harry Svensson
5 hours ago
Ah oops, added the backticks
– Jet Blue
5 hours ago
add a comment |Â
shouldn't it belogic [5:3] opcode;
? - Ah wait, no, it's packed. of course. I presume that order of definition matters. - Is there another kind of struct for doing what I just said? some absolute addressing?
– Harry Svensson
5 hours ago
Ah oops, added the backticks
– Jet Blue
5 hours ago
shouldn't it be
logic [5:3] opcode;
? - Ah wait, no, it's packed. of course. I presume that order of definition matters. - Is there another kind of struct for doing what I just said? some absolute addressing?– Harry Svensson
5 hours ago
shouldn't it be
logic [5:3] opcode;
? - Ah wait, no, it's packed. of course. I presume that order of definition matters. - Is there another kind of struct for doing what I just said? some absolute addressing?– Harry Svensson
5 hours ago
Ah oops, added the backticks
– Jet Blue
5 hours ago
Ah oops, added the backticks
– Jet Blue
5 hours ago
add a comment |Â
up vote
0
down vote
Yes, you can.
But it's probably cleaner and easier to simply break the register into fields using wires:
wire [2:0] a_opcode = a[5:3];
if (a_opcode == someValue) ...
How many different registers are you going to be extracting opcodes from?
Note that this solution only works for reading a_opcode, not writing to it. Most of the time, that is OK.
– dave_59
6 hours ago
add a comment |Â
up vote
0
down vote
Yes, you can.
But it's probably cleaner and easier to simply break the register into fields using wires:
wire [2:0] a_opcode = a[5:3];
if (a_opcode == someValue) ...
How many different registers are you going to be extracting opcodes from?
Note that this solution only works for reading a_opcode, not writing to it. Most of the time, that is OK.
– dave_59
6 hours ago
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Yes, you can.
But it's probably cleaner and easier to simply break the register into fields using wires:
wire [2:0] a_opcode = a[5:3];
if (a_opcode == someValue) ...
How many different registers are you going to be extracting opcodes from?
Yes, you can.
But it's probably cleaner and easier to simply break the register into fields using wires:
wire [2:0] a_opcode = a[5:3];
if (a_opcode == someValue) ...
How many different registers are you going to be extracting opcodes from?
edited 20 mins ago
answered 7 hours ago
Dave Tweed♦
109k9132236
109k9132236
Note that this solution only works for reading a_opcode, not writing to it. Most of the time, that is OK.
– dave_59
6 hours ago
add a comment |Â
Note that this solution only works for reading a_opcode, not writing to it. Most of the time, that is OK.
– dave_59
6 hours ago
Note that this solution only works for reading a_opcode, not writing to it. Most of the time, that is OK.
– dave_59
6 hours ago
Note that this solution only works for reading a_opcode, not writing to it. Most of the time, that is OK.
– dave_59
6 hours ago
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2felectronics.stackexchange.com%2fquestions%2f397648%2fverilog-can-you-define-a-bit-slice%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password