ChanServ changed the topic of #yosys to: Yosys Open SYnthesis Suite: https://github.com/YosysHQ/yosys/ | Channel logs: https://libera.irclog.whitequark.org/yosys/
tpb has quit [Remote host closed the connection]
tpb has joined #yosys
lexano has quit [Ping timeout: 250 seconds]
skipwich_ has quit [Quit: DISCONNECT]
skipwich has joined #yosys
peepsalot has quit [Remote host closed the connection]
peepsalot has joined #yosys
citypw has joined #yosys
krispaul has joined #yosys
kristianpaul has quit [Ping timeout: 252 seconds]
lexano has joined #yosys
napaalm has joined #yosys
<napaalm> Hi, I have an issue with yosys synthesis, which results in a different behaviour than the input file:
<napaalm> module test(clk, rst_n);
<napaalm>     input clk, rst_n;
<napaalm>     reg STAR;
<napaalm>     localparam S0=0, S1=1;
<napaalm>     always @(rst_n==0) STAR <= S0;
<napaalm>     always @(posedge clk) if (rst_n==1)
<napaalm>         casex(STAR)
<napaalm>             S0: STAR <= S1;
<napaalm>             S1: STAR <= S0;
<napaalm>         endcase
<napaalm> endmodule
<napaalm> executing
<napaalm> $  yosys -p "synth -auto-top; write_verilog synth.v" test.v
<napaalm> results in the following file:
<napaalm> (* hdlname = "\\test" *)
<napaalm> (* top =  1  *)
<napaalm> (* src = "test.v:1.1-12.10" *)
<napaalm> I see that my previous message is not so readable, so I uploaded the input and output files to pastebin
<napaalm> Input file: https://pastebin.com/1ZzSkp7H
<napaalm> Command: yosys -p "synth -auto-top; write_verilog synth.v" test.v
<tpb> Title: test.v - Pastebin.com (at pastebin.com)
<napaalm> Output file: https://pastebin.com/6p1G2bQ6
<napaalm> The yosys synthesis is wrong, because STAR is no longer a register and its value doesn't change on clock positive edges. Can anyone give me some info on why this happens? Thank you!
<tpb> Title: synth.v - Pastebin.com (at pastebin.com)
<corecode> is it okay to have two processes assign to the same register?
<corecode> i wouldn't write it that way
<napaalm> That is how verilog RTL it's taught at my university. Could you please show me a better way to write it?
<corecode> hm maybe always @(posedge clk, negedge rst_n) if (!rst_n) STAR <= 0 else STAR <= !STAR;
<corecode> if you write two processes, you have two drivers; one is a latch, the other a register
<corecode> i think
<napaalm> Unfortunatly I don't know what exactly is a process, in this context
<corecode> ZipCPU might have an opinion
<corecode> always block
<napaalm> Oh I see
<corecode> do you write tests?
<napaalm> Yes, if I simulate my code with a simple testbench using iverilog, the behaviour is as expected
<napaalm> I can show you
<corecode> not necessary
<corecode> simulators don't always exhibit the exact same behavior when the code is incorrect
<corecode> did you not get any message about two drivers for one signal?
<napaalm> I did not
<corecode> try to set everything to maximum warns and lints
<corecode> this could help?
<napaalm> iverilog with -Wall doesn't output any warning
<corecode> maybe ask in ##fpga
<corecode> this channel is pretty quiet
<napaalm> Ok, in case this code style is actually correct, isn't this a bug in yosys?
<napaalm> The linter you just linked my also doesn't show any warning: I just tried
<corecode> hm
<corecode> maybe it is valid code because of the conditions?
<corecode> seems unlikely tho
<napaalm> I don't know, my verilog knowledge is very limited, I'll try asking in the other channel, as you suggested. Thank you
napaalm has quit [Quit: Client closed]
napaalm has joined #yosys
napaalm47 has joined #yosys
napaalm47 is now known as YourNick
YourNick has left #yosys [#yosys]
<whitequark> corecode: no, it is not okay to have two processes assign to the same register
<whitequark> oh, sorry, I missed a bit of context
<napaalm> So, is my code incorrect?
<whitequark> yes. there are at least two issues with it
<whitequark> first: you cannot use two processes to write to a register with yosys (or any other synthesizer I know)
<whitequark> second: yosys does not support conditions like always @(rst_n==0)
<whitequark> I think the specific behavior is that it is treated like always @* in this case
<whitequark> but it is an error (which is not diagnosed by yosys) to use a condition like that
<napaalm> Could it be an old syntax? Because here at my university we are taught this exact way to write RTL code
<napaalm> Is the following code correct? https://pastebin.com/zm3cqcVU
<tpb> Title: test_corrected.v - Pastebin.com (at pastebin.com)
citypw has quit [Ping timeout: 240 seconds]
srk_ has joined #yosys
srk has quit [Ping timeout: 246 seconds]
srk_ is now known as srk
<ZipCPU> napaalm: Yes, your last pastebin will work.
<ZipCPU> The code you wrote initially is only legal in a simulation context. Verilog was initially written as a simulation language, and so it can work in simulations.
<ZipCPU> However, you can't map it to hardware.
<ZipCPU> When it comes to mapping a design to hardware, you have to limit yourself to only the constructs that the hardware supports, and assigning the same register in two separate always blocks is ... well, that doesn't describe any hardware.
<ZipCPU> It only has meaning in a simulation.
kraiskil has joined #yosys
<ZipCPU> If that's the kind of logic taught you in your school, then either 1) you are taking a class in how to write Verilog simulations (check your syllabus), 2) you missed a key piece of context in the lesson, or 3) you instructor has been delinquent about telling you the difference.
kraiskil has quit [Ping timeout: 245 seconds]
<napaalm> Thank you ZipCPU, now it all makes sense! During the class we never touch actual hardware, instead we write simulations with iverilog
<ZipCPU> napaalm: You aren't the first person with this type of question.
<ZipCPU> Indeed, its a common beginners mistake. I see this sort of thing all the time.
<napaalm> I can imagine that
<ZipCPU> It's also why I limited my tutorial to *only* those parts of Verilog that could be synthesized.
<napaalm> Speaking of code that can't be synthesized, now I am trying to synthesize tri-state logic, but I can't get yosys to convert it to a form that can be synthesized to an fpga. I don't understand the exact purpose of the "tribuf" command, and it doesn't seem to make a difference in my code
<ZipCPU> Heh. Yeah, that's another one.
<ZipCPU> The trick is there's only a couple ways to do it, and a lot of ways to mess it up.
<ZipCPU> Properly, you might write: assign out = (tristate) ? 1'bz : value;
<ZipCPU> But beware, you can *only* do that to output of the chip at the top level.
<ZipCPU> The other way to do it is to instantiate the FPGAs tri-state IO buffer.
<ZipCPU> Which buffer you instantiate, however, depends upon the FPGA you are using.
<napaalm> Okay, so this applies only to top-level outputs, and what about internal logic?
<ZipCPU> Internal tristate logic is only supported by some daring synthesis tools.
<napaalm> My FPGA is a Lattice ECP5
<ZipCPU> It's not universally supported.
<ZipCPU> See, the thing is, there's _NO_SUCH_THING_ as internal tristates on an FPGA. The hardware doesn't allow it.
<ZipCPU> Some synthesis tools will attempt to approximate it via logic, but the hardware for it doesn't exist.
<ZipCPU> (I don't mean to yell, but if I were an instructor I would've given a proverbial foot stomp at that, and ... I'm not sure how to write that in IRC text ...)
<napaalm> Okay, I had found online this limitation of FPGAs, so the question is: can yosys do this kind of approximation?
<ZipCPU> I haven't tried it. I think some of the developers have said it could, but it wouldn't be a widely used or tested feature so ... your mileage might vary.
<ZipCPU> In my case, I'm always writing to the lowest common denominator, so if there's one synthesizer my customers might use that doesn't support a feature, I'm stuck without it.
<ZipCPU> Sometimes I think they bought commercial tool XYZ back in the early 2000's, and they couldn't afford to update the licenses ... so, the market I work with leaves me stuck w/o a lot of language features others really like.
<napaalm> I see, it doesn't seem like an easy field to work in
<ZipCPU> It's not all that bad. You just have to learn the few constructs that are universally supported.
<lofty> napaalm: pretty much whenever you use tristate logic then Yosys will issue a warning about limited support for it.
<lofty> But, really, there's no real need for tristate logic internally?
<ZipCPU> I agree with lofty. There's no reason to use it internally.
<napaalm> Yeah, I am trying to synthesize an educational CPU we studied in class, which has an internal bus to connect to memory and IO space
<napaalm> I would like to avoid manually reimplement that code without tri-state logic, that's all
<lofty> I assume your mental model was tristate buffers to control read/write to a common set of wires
<ZipCPU> Technically, ASICs can support internal tristates. That doesn't mean they're good ideas to use. However, there was a past generation that used them more liberally.
<napaalm> lofty: Yes, it's a common bus for all devices
<lofty> Even if tristate buffers were supported in internal fabric, they were *very* slow
<ZipCPU> To use a tristate bus in an FPGA, you have to keep a copy of the bus for each potential source, together with the tristate enable for each source, and then you use a LUT (or two or three) per bit to determine the bus value.
<ZipCPU> It's often easier for a two-way bus to simply keep track of both directions independently, rather than those plus the logic necessary to merge them.
<lofty> napaalm: instead what tends to happen is the CPU has separate read/write buses; all devices read from the CPU's write bus, and their write output is multiplexed based on a device's validity signal
<lofty> ZipCPU: Well, all ASICs use internal tristates due to the nature of a transistor, but that's being pedantic
<napaalm> Okay, it makes sense. I thought that yosys could autonomously do this kind of transformation
<ZipCPU> I was trying to describe how you'd build an internal tristate within an FPGA, but ... okay.
<napaalm> Do you know what's the exact purpose of this? https://yosyshq.readthedocs.io/projects/yosys/en/latest/cmd/tribuf.html
<tpb> Title: tribuf - infer tri-state buffers (at yosyshq.readthedocs.io)
<lofty> That's pretty much for I/O buffer inference
<napaalm> And what about the option "-logic"?
<ZipCPU> Not sure. Try it.
<lofty> "convert tri-state buffers that do not drive output ports to non-tristate logic."
<lofty> AKA, infer an input buffer rather than a tristate buffer if possible
<napaalm> I did try (and it's already included in the synth_ecp5 command), but doesn't help in my case: at the end of the synthesis I get many warning about multiply driven ports
<ZipCPU> Warning, schmorning. If it's not an error, it did something
<ZipCPU> Unlike software, warnings are quite plentify in hardware design
<ZipCPU> Few tools will let you synthesize without generating them
<lofty> Yeah, uh, I would treat a multiple-driver warning seriously, unlike ZipCPU
<napaalm> Yeah, if I then proceed to place and route I get an actual error
<napaalm> nextpnr-ecp5 --json computer.json --textcfg computer_out.config --85k --package CSFBGA285 --lpf orangecrab_r0.2.pcf --lpf-allow-unconstrained
<napaalm> ERROR: Net 'C.d7_d0[0]' is multiply driven by cell ports C.P.D7_D0_TRELLIS_FF_Q_7.Q and C.SdIO.Switch.RBR_TRELLIS_FF_Q.Q
<lofty> Because in the best possible case that's unimplementable
<ZipCPU> lofty: ;) Normally I'd treat it seriously too.
<lofty> And in the worst case you have a bitstream that will produce magic smoke
<lofty> This is...less common these days though, because FPGAs don't have tristate buffers in them
<ZipCPU> Do we need to explain to napaalm how these FPGAs run on smoke?
<ZipCPU> This stuff needs its smoke to run. If you ever let the smoke out, it stops working.
<napaalm> Hahahah
<napaalm> So, it seems I don't have much choice but to implement two separate buses
<lofty> Correct
<napaalm> Thank you very much for your help ZipCPU and lofty !
<ZipCPU> ;)
<ZipCPU> Good luck.
<napaalm> Thanks
* ZipCPU goes back to working on his 10Gb Ethernet, trying to configure those GTX transceivers just right ...
<lofty> napaalm: no worries
* ZipCPU can't find a valid way to use Xilinx's 66B/64B gearboxes, grumbles and goes on to write his own ...
<whitequark> ZipCPU: there are some old FPGAs that do have internal tristates
<whitequark> I'm not sure whether they exposed those, but architecturally some old Xilinx devices did have them
<ZipCPU> Seriously? Wow. I have just learned something. Do you remember which ones?
<whitequark> mwk would know for sure, but I think it was... xc2s, maybe?
<whitequark> I believe internal tristates were used to save die area on interconnect
<whitequark> and they stopped using those because doing timing driven routing where you have to consider that your capacitance changes depending on the pass transistors you enable isn't tractable
<whitequark> "One third of the Hex lines are bidirectional, while the remaining ones are unidirectional."
<whitequark> "12 Longlines are buffered, bidirectional wires that distribute signals across the device quickly and efficiently."
<whitequark> there's even a picture saying "3 state lines". so yep, that one has it
<whitequark> those bidirectional buffers look like an absolute nightmare to me... it's amazing they got it to work reliably
<ZipCPU> Wow. Cool. That explains a lot though.
<ZipCPU> It also explains why there's logic around that uses these things, when they are (apparently no longer) synthesizable.
<whitequark> I don't have a way to check but it seems like you could use BUFT internally
<whitequark> yeah looks like you could
<whitequark> also the name implies it's solely for internal routing (or it would be an OBUFT/IOBUFT)
<lofty> The Pilkington FPGA has internal tristate buffers too
<lofty> But I think they were intended for logic more than anything
nonchip has quit [Quit: https://quassel-irc.org - Chat comfortably. Anywhere.]
nonchip has joined #yosys
<mwk> whitequark: xc3k, xc4k, xc5k, xcs, xc2s, xcv, xc2v and all variants thereof
<whitequark> wait, xc5k?
<mwk> xc3k, xc4k, xc5k, xcs have real tristates IIRC; the ones on xcv, xc2s, xc2v are... kinda emulated in hardware, but in a fast way
<mwk> xc5200
<whitequark> huh
<mwk> an old family of cheap FPGAs
<whitequark> how are they emulated?
<mwk> xilinx actually described it in a patent
<mwk> basically... well, like you'd expect
<mwk> it's a wired-and, except with a real and gate instead of the wired part
<mwk> in short: every CLB has 4 horizontal tristate lines going through it; each horizontal tristate line is splittable into 4-CLB segments
<mwk> and each CLB has two TBUFs (with T and I inputs), each of which has two tristate lines that it can drive
<mwk> there are programmable joiners which basically boil down to chaining the AND gate inputs in both directions or not
<mwk> also I very vaguely recall old lattice (maybe even AT&T era?) FPGAs having tristates too, but...
<mwk> oh, and also
<mwk> the thing about timing and routing is
<mwk> before xc2v, the interconnect in xilinx FPGAs wasn't fully *buffered*
<mwk> so the timing analysis had to actually compute things like capacity of each unbuffered pass-transistor-joined segment of the network