Coder Social home page Coder Social logo

new condtion syntactic suga about spinalhdl HOT 6 CLOSED

jijingg avatar jijingg commented on June 9, 2024
new condtion syntactic suga

from spinalhdl.

Comments (6)

Dolu1990 avatar Dolu1990 commented on June 9, 2024

Hi @jijingg,

I think there is something quite close already via the spinal.lib.WhenBuilder :

    import spinal.lib._

    val conds = Bits(8 bits)
    val result = UInt(8 bits)

    val ctx = WhenBuilder()
    ctx.when(conds(0)) {
      result := 0
    }
    ctx.when(conds(1)) {
      result := 1
    }
    if(true){
      ctx.when(conds(2)) {
        result := 2
      }
    }
    ctx.when(conds(3)) {
      result := 3
    }

    for(i <- 5 to 7) ctx.when(conds(i)) {
      result := i
    }

    ctx.otherwise{
      result := 255
    }

=>

  assign when_Utils_l1437 = conds[0];
  always @(*) begin
    if(when_Utils_l1437) begin
      result = 8'h0;
    end else begin
      if(when_Utils_l1440) begin
        result = 8'h01;
      end else begin
        if(when_Utils_l1440_1) begin
          result = 8'h02;
        end else begin
          if(when_Utils_l1440_2) begin
            result = 8'h03;
          end else begin
            if(when_Utils_l1440_3) begin
              result = 8'h05;
            end else begin
              if(when_Utils_l1440_4) begin
                result = 8'h06;
              end else begin
                if(when_Utils_l1440_5) begin
                  result = 8'h07;
                end else begin
                  result = 8'hff;
                end
              end
            end
          end
        end
      end
    end
  end

  assign when_Utils_l1440 = conds[1];
  assign when_Utils_l1440_1 = conds[2];
  assign when_Utils_l1440_2 = conds[3];
  assign when_Utils_l1440_3 = conds[5];
  assign when_Utils_l1440_4 = conds[6];
  assign when_Utils_l1440_5 = conds[7];

More syntax suggars can be added

Would this match the need ?

from spinalhdl.

jijingg avatar jijingg commented on June 9, 2024

@Dolu1990 Good, this meets my need for parameterization. It's indeed a feature that's not easy to discover, so it's necessary to add it to the documentation.

from spinalhdl.

Dolu1990 avatar Dolu1990 commented on June 9, 2024

Yes i agree, this is quite underground lacking doc.

from spinalhdl.

jijingg avatar jijingg commented on June 9, 2024

Yes i agree, this is quite underground lacking doc.

@Dolu1990 don't worry, I will add it to the document later.

i have another question , Is there a way to move the previous expression to the later , some thing like this ,

  //#PART1
  when(io.addr === 0x0004 && io.wr){
    my_reg := io.wdata
  }.elsewhen(io.clr){
    my_reg  := B(0x1234, 32 bit)
  }
...
...
...
  i know my_reg have many method  like `my_reg.removeStatement(`)
  did there have mothod to get `my_reg.getStatement()` then move to another place
 
 //#PART2
  when(io.set){
    my_reg  := B(0x1234, 32 bit)
  }.elewhen(xxxx){
    my_reg.move_part1_logic_to_here
  }.otherwise{
   my_reg  := B(0x0000, 32 bit)
  }

Of course, this feature is not recommended for ordinary users to use, but it is still necessary to reserve such interfaces for areas that require hacks.

from spinalhdl.

Dolu1990 avatar Dolu1990 commented on June 9, 2024

I will add it to the document later.

Thanks :D
Also feel free to tweek / add syntax sugar on the top

my_reg.move_part1_logic_to_here

So, moving things using my_reg as "anchor" is tricky , because it would not be realy a move, but more a "move statments into a duplicated conditional scope".

I would say that instead, moving the whole when statements would be easier, but there is nothing in place for this yet. But it would just be about patching the linkedlist of statements i think. But there may be some tricky side effects related to statements ordering, i'm not sure.

Otherwise :

def func(){
  when(io.addr === 0x0004 && io.wr){
    my_reg := io.wdata
  }.elsewhen(io.clr){
    my_reg  := B(0x1234, 32 bit)
  }
}
 //#PART2
  when(io.set){
    my_reg  := B(0x1234, 32 bit)
  }.elewhen(xxxx){
    func()
  }.otherwise{
   my_reg  := B(0x0000, 32 bit)
  }

from spinalhdl.

jijingg avatar jijingg commented on June 9, 2024

I would say that instead, moving the whole when statements would be easier, but there is nothing in place for this yet. But it would just be about patching the linkedlist of statements i think. But there may be some tricky side effects related to statements ordering, i'm not sure.

Indeed, it might not be easy to handle, and it could be more feasible to choose a def method as a callback. Thanks

from spinalhdl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.