Custom Chips: Test any digital circuit in your browser with Wokwi

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello when you have a digital circuit you'd like to get running one approach is to SketchUp a design and prototype say on a breadboard but to get started you'll need all the components to wire it up correctly and then you can write up some code if you've got a microcontroller and get that installed and that's a lot of steps uncertainties and time cycling through the development load it could be a lot simpler and faster to get a simulation running instead to test and tweak things on a computer before busting out the jumper wires now most projects I designed for the real world involve a microcontroller and a number of peripheral chips each MCU has its own best way of simulating it but the real sticking point is those peripherals to get a simulation going you need to find a way to model those as well and then get all the pieces connected and speaking to each other now today I'm going to show you an easy way to do all this with walkway an integrated system to run simulation that supports mcus you're probably using has a number of peripherals built in but also allows you to Define any custom chips to use in your projects we'll start really simple and work up through a couple of interesting projects so let's start a new project on the homepage there are a lot of choices to start from but for something really basic like this I like to use a new blank then I save a copy for my own use okay what we're going to do is Implement a custom chip one that just counts clicks and shows us visually so I'm going to have a button and to keep part count down just use a bar graph now all I need is our custom chip so add custom chip use the recommended C API and call it very originally test so two files are added to the project a Json file that describes the Chip and A C source file to implement it inside the Json the pin array defines the number of pins and they're naming so I'll replace that within one two three four now you can see the breakout has changed to match this the order determines the pin number here so the first one is pin one Etc if you want to skip a number you can just add empty strings this can let you ensure your custom chip matches any real life I see you're modeling okay that looks good now the really interesting part is the implementation this is where the magic happens the implementation file has some boilerplate to help you everything of interest will be happening in callbacks so the chip State structure is where we'll stash our state information the chip in it is you guessed it where we initialize everything so let's code something up let's say I want to count up to four the number of output pins I set in the Json so I'm going to want to keep track of the current count so I'll stick that in the chip State structure are you an eight since it's a small value I'll also be playing with those output pins so let's keep an array of these which will be pin type things really just an end but this is clean okay on to the init in here we do all our setup I don't like repeating myself so I create an array of all the output names initialize each pin as an output and stash the return reference ID thing for this output in my state we also have an input so let's initialize that get a handle to that pin while initializing it as an input with a pull down so it's low by default okay so those outputs are simple beasts we'll just be writing to them whenever we want but the input is something else we don't know when that'll come in so we need to treat it differently the documentation shows all the details and I'll use that as a starting point and now we tell the system we want to watch it for this we create a watch config structure we want to monitor Rising Edge events we want to call input changed whenever that happens and that will get our chip structure as the user data now all we need to do is call pinwatch with this pin and config and this won't build however because we still need the Callback okay so we call that input changed we get handed the user data but the system doesn't have a clue what that is so we cast it uh old school c-style to our chip stay so every time this gets called the user pushed the button we're going to up the count modulo the number of outputs reset all the outputs to off and set the output for the current count high if it's between 1 and 4. okay our counter notice I put a printf statement as a breadcrumb to see what's happening just don't forget the new line slash n or you won't see anything because buffer reset all outputs we use pinwrite to set them low and here if the chip count is between one and four we write high on the appropriate output I guess we should wire this up so something you may notice here is that I'm not actually powering this chip and there's no current limiting to the LEDs in real life that would kill them but walkway is pretty forgiving in this Regard in a real Chip model I'd add power pins and current limiting just out of principle and to Future proof things but you don't have to okay we've got a test chip in a circuit let's give it a go well that's kind of working in fact it looks more like we're rolling dice than counting you can see that every time I click the count jumps through multiple steps so this is a consequence of block we actually modeling the push button pretty nicely in real life these springy things bounce around and cause a bunch of triggers I like the realism but here we don't need it so I'm going to set this attribute to zero in the Json definition for the push button side note though I tend to play in the Json directly this can be changed easily through the GUI okay if we run this noise so there you have our first custom chip running okay now let's do a real Chip say I have a project with the venerable 4511 which takes a nibble in and drives a seven segment display okay there are a lot of these and any data sheet will do but there's a nice description on build electronics.com so it takes four bits and will deal with driving a seven segment display another new blank project will have a seven segment display dip switch for the inputs and a custom chip okay set up the pin so they match the real Chip the resulting custom chip looks like this now okay one more little tweak and I can wire this up that seven segment display needs to be common cathode so I'm going to set that in the parts attributes it's wired up okay so I've made this more colorful to clarify things the first four switches on the dip are connected to the input bits in order I've ignored the blank and test pins but did tie the latch enable here the seven segment is all wired up with 8A Etc okay let's start implementing for the state we're going to be watching the inputs playing with the outputs and keeping track of whether the latching is enabled here I've set up a more interesting chip State structure I put an arrays for the input output pin separated out the control pins in the substructure and we'll keep track of the value from the in bits in a single place called value let's submit all this stuff I've got little Loops to in it and store the inputs outputs let's also keep track of the latch enable okay we need to keep an eye on all these inputs the latch enable is just like push button in the last example and for the inputs I'll just shoot them all over the same input change callback finally I want to check the latch enabled during init because walkway will keep my switch settings between runs and we can assume that it'll be low but also low means enabled here so we want to track that so now I've set things up to use three functions that don't exist yet latch enable input change callbacks and something to update the output segments first the input changed so this is what our function looks like I'm going to keep things simple here and just read all the inputs on any change so let's get the chip state so we can have all our pin handles here's a quick Define that'll do the casting and Chip star instantiation from the void user data okay let's Loop over all the pins and read in the value finally if it's enabled let's use that update output function again time to actually Define that so here I've got an array that Maps any number between 0 and F 15. to the segments we want on I'm using this update output internally so actually passing the chip State pointer around directly we'll get the current value get the segment mapping and just turn on any segment that has a one set finally let's do the latch enable change callback it just sets the inverted logic enabled flag and if it was just enabled we'll update the output right away let's give it a go you can click the div switches or when the part is selected just use the keyboard so that's looking good let's try latch enable I said hi to disable change the input nothing happens but when I stop disabling it okay I think we're good cool so this is a realistic simulation of our chip but setting that input one bit at a time is somewhat annoying if you want to simplify things or have internal configuration state that you want to be able to tweak custom chips also provide an easy way to do that controls controls provide attributes that you can set during the simulation let's say we want to be able to override the input value so we'll Define a control object in the Json with the simulation running we can now see this control slider by clicking on the chip doesn't do anything yet though so let's initialize this attribute and use it in the implementation inside our chip in it we'll get a handle to this attribute okay to keep an eye on it this time we'll use another custom chip feature a timer like the pin watch we init that with a struct holding details here we'll have a manual value checked callback so init that and starter up the 5000 is microseconds to uh timeout so five milliseconds and the truth is whether this should be periodic and repeat then we want that okay so let's implement the check okay get the current value of the read um I still want the input pins to work so let's only override this if the manual value has changed so let's update the state we need the attribute in there as well as the actual value all right so if it has been updated we track that change update the actual value recorded and update the output let's try that out nice that works but doesn't impact actual function most interesting chips will have some way other than parallel bits to control them but you don't want to be spending your time implementing a low level I squared C drivers thankfully the custom chips provide easy support for SPI I squared c u art even analog uart is really easy to implement but I'm currently working on this project I made myself a little tool to suck the netlist from keycad into walkway and I'll talk about that later but you can see this circuit has a w25 Q32 custom chip here that's a Serial flash memory I'd like to support that chip and it uses SPI focusing on developing just that IC we'll use an MCU to control the device and begin by implementing a simple little client custom chip that speaks through SPI to get things going okay so we've got our chip set up with various pins and in the implementation the first thing we're going to want to do is keep an eye on the chip select so let's add that to the chip State structure we'll also need a handle to the SPI device and a buffer for that so I add these in the init we get our chip select and set up the watch for chip select pin as we've done before the SPI configuration and it looks similar to everything we've done before but it has a few more attributes basically to tell it which pins to use as clogged meso mozzie plus the mode now the Callback here is called done and that's an indication of something important so we init that Implement these callbacks I create a little utility function to tell me if the Cs is asserted it's inverted logic now in the select changed if this chip is actually selected then we call SPI start to trigger reception transmission we pass that the SPI device handled the buffer and the number of bytes to receive and send now if the chip isn't selected well the Cs just changed so it must mean that we've been selected before so we call SPI stop now every time CS goes low we're going to start receiving a single byte once that has actually come in the done call back will be triggered so let's do that so first thing this gets called after stop two if that's the case count is zero here so we just return okay we're done okay we got a bite now I'm not going to care about it except uh to print it out just right now um but what what are we sending back uh let's put in a next response function to do that later finally and this is important if we're still selected call start again to ensure that we get the next bite okay last thing is to actually write the next response function I'll keep track of how many bytes we've processed already and just shoot back the next character in some arbitrary string so that should do it for our device now we need to drive this so in an Ino file a basic Arduino thing in the setup we want to have the chip select as an output and not have the chip selected yet so write a high and then the loop we'll just select transfer a number of characters and spit out what we got back from the chip let's try this out okay that's looking pretty good the driver is getting back the responses and on the chip side one two three four five six is indeed being received nice now that we have both sides talking with each other over SPI it just becomes a matter of implementing the actual API of the chip itself if you want to try it out check out the documentation which contains number of samples and the examples shown here are all Linked In the description that should be enough to get you started with your own custom chips so you can head to walkway.com right now and try it out in the next video I think I'll dive into actually implementing a non-trivial custom chip probably using I squared C so stay tuned for that have fun thanks for watching
Info
Channel: Wokwi
Views: 114,018
Rating: undefined out of 5
Keywords: arduino, esp32, raspberry pi, simulation, custom chips, custom IC, tutorial, walkthrough
Id: yzdCS3A4DvU
Channel Id: undefined
Length: 14min 39sec (879 seconds)
Published: Fri Aug 11 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.