Verilog - Can you `define a bit slice?

The name of the pictureThe name of the pictureThe name of the pictureClash 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









share|improve this question



























    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









    share|improve this question

























      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









      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 5 hours ago

























      asked 7 hours ago









      Jet Blue

      459314




      459314




















          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].






          share|improve this answer






















          • 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

















          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?






          share|improve this answer






















          • 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










          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("schematics", function ()
          StackExchange.schematics.init();
          );
          , "cicuitlab");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "135"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          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






























          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].






          share|improve this answer






















          • 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














          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].






          share|improve this answer






















          • 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












          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].






          share|improve this answer














          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].







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 21 mins ago









          Dave Tweed♦

          109k9132236




          109k9132236










          answered 6 hours ago









          dave_59

          1,598614




          1,598614











          • 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
















          • 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















          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












          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?






          share|improve this answer






















          • 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














          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?






          share|improve this answer






















          • 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












          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?






          share|improve this answer














          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?







          share|improve this answer














          share|improve this answer



          share|improve this answer








          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
















          • 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

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          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













































































          Comments

          Popular posts from this blog

          What does second last employer means? [closed]

          List of Gilmore Girls characters

          Confectionery