Asteroids on Oscilloscope

The Joy of Tech - Anything and everything about all things shiny..
Post Reply
User avatar
Kamineko
Baaaaaaa!
Baaaaaaa!
Posts: 7569
Joined: Fri Dec 17, 2004 2:53 pm 0
Contact:

Re: Asteroids on Oscilloscope

Post by Kamineko »

The DAC chip arrived yesterday, just as foretold. Couldn't believe it. They were way too kind.

I was in two minds about whether to solder the chip directly to the board or get a socket for it. If it had arrived a little later in the day, I'd have used it as an excuse to go to the city centre and look in Maplin, but instead I was ill and my awesome mum kindly looked out for the Fedex package for me. I decided to just solder the chip, I'd done it before with the amp board and it seemed to work out fine. There was really no reason not to, unless I somehow installed the chip the wrong way around.

The dsPIC33's modules are mostly pin remappable (mostly the various intercommunication busses). The inbuilt audio DAC wasn't though, it was fixed at the upper right. When it's disabled, the pins act like any other: they can be used for analogue in, remappable modules or GPIO. It was a puzzle to fit the chip on top of the holes I'd gouged in the traces on the board. Eerily, the four holes I'd gouged in the board so I could mount resistors between DAC out and the pin headers were in exactly the right place to mount the 8-pin DIP DAC chip across. All I had to do is route power and ground, make REF, and figure out a way to put the two output channels beside each other for the pin header.

The DAC chip has the data interface (DATA, CLOCK, CHIP SELECT) on the left together with the primary out channel (why is it on that side, WHY). The other side has VDD, OUT B, REF, GND. I was thankful I ordered all the spare resistors I did, because I had to make 1V from 3.3V at the REF pin.

After installing it, and praying I hadn't blown the poor guy up, I then had to figure out how to communicate with it. I've never done SPI before, but it looked like I had the choice of triggering each word write manually by writing to a memory mapped register, or setting up an asynchronous DMA channel that would spew out words from a double buffered array in memory and raise interrupts whenever it was switching buffers.

I spent about an hour going between the two datasheets, trying to find a pair of settings that correlated. The DAC datasheet was really vague about whether or not it wanted you to toggle the chip select line up and down every time you transmit a word. A lot of SPI related guides say you should just hold the chip select line low always, but I kept it connected to the PICs GPIOs anyway in case I needed it. I reckoned the DAC chip did need a toggle, because there's a 'minimum chip select line held high between words' time given in a table somewhere else in the document. It's not shown on the timing diagram, and the timing diagram only shows the transmission of a single word. The dsPIC has a mode called 'framed SPI' which pulses the chip select between words, so after I matched up what I needed with what I could supply it all looked good to go.

It didn't work at first. The examples in the data sheets only give you parts of the final answer.

The final problem would have had me stumped for weeks if I hadn't been lucky enough to stumble on both how to identify the problem and solve it. The Ocelot has a LED on the front that I can control in software. Without the screen, it's the only visible feedback I can get from the machine while it's running. After noticing I wasn't getting anything on the scope, I decided to put an infinite loop in all my interrupt routines to see which ones were firing. If the interrupt fired, it would hit the loop and the light would get stuck. The DMA interrupt wasn't firing. This one is supposed to fire every time a block of 128 bytes (64 SPI words, or 32 pairs of stereo DAC instructions) has been transferred. For some reason, the dsPIC wasn't spewing out the data. The pin assignments were correct, and I was seeing the clock in the correct place (framed SPI runs the clock all the time, unlike normal SPI which stops the clock when not transmitting), but nothing was happening.

So I went online and looked for everybody who used PICs whose DMA interrupts ceased to function for unknown reasons.

It turns out that the solution to this is due to what I consider an oversight in the design of the dsPIC's SPI configuration. SPI is always bidirectional according to the PIC's model. If you have two sister chips communicating through SPI, each will be shifting bits out while at the same time receiving bits in. If you want to receive bits without transmitting them, the recommended way to do this is to enable a 'dummy write' in the DMA, where it'll pretend to be outputting data through SPI when it actually isn't.

There's no analogous configuration for just transmitting data though. The datasheet and the programming SPI guides don't mention this. They say you can have a just transmitting configuration if you follow the normal routine of enabling SPI, assigning pins, and setting up a DMA channel to ferry the data. But what it doesn't tell you is that that won't work. You're not receiving bits. Or rather, you're not handling the bits you're receiving, even if you're receiving phantom bits because you haven't assigned a physical pin to the DATA IN line. If you don't handle the bits you're receiving, that's an overflow event. The overflow event will raise a flag, and that will halt the entire SPI module until you clear it manually.

The recommended solution goes a bit like this: you either have to set up a second DMA channel (wasting it) to write the phantom bits to DMA-accessible memory (wasting it), and have stub interrupt routines that do nothing but waste time...

Or you can simply manually read from the SPI buffer in the SPI interrupt you already have that fires every time you receive a word. You'll be servicing that interrupt every time you transmit a word anyway, so there's nothing to be wasted by using it to also acknowledge the word you 'received'. unsigned int x = SPI1BUF;, and every problem simply disappears. I like my solution. :3

Once that was in place, it was GLORIOUS. Unlike the dsPIC's DAC, this TI chip gets your instructions and FRIGGIN' NAILS the voltage straight off. None of this 'oh, I'll give it a go, but I'll probably miss and it'll be a bit shaky'. No! You want 0.458 volts? BAM. DONE. NEXT. You want 0.2583V there and 1.564V over there at the same time? DOUBLE BAM.

Image
You can barely see it on the right, but the DAC chip has some resistors to the right that make the 1V REF it needs (the DAC outputs between 0v and double your REF, as long as the max result is 1.2V lower than supply.).

It's done. I can't believe it's actually done. This is the final Ocelot mainboard, barring any oversights. (And there will have been some. There's been plenty so far.)

It's got Zipstick and Mega Drive input, four channel square wave sound, drives an oscilloscope in XY mode, runs off 3xAA batteries and gives you 40 magnificent MIPS of gaming goodness.

The improved regulator means there's low enough drop out for it to actually function off batteries, and there should be plenty of spare juice to support the Amiga mouse after all (provided it can be run off 3.3V not 5V).

Image

The only things left to do now are to write some kick-ass software for the beast and make a case for it! The DoES guys keep saying I should pop down and make a nice laser cut case, but I didn't want to get carried before I'd made it functional in case I had to change the physical part of it for some reason (like the phono jacks being wrong or too capacitancy or too mushy or something I couldn't have possibly accounted for).

For software, first up I have to write a library to help me draw stuff to the screen. I have two approaches in mind, I can either write all my instructions and value directly into the final value buffers and have the DMA interrupt basically do nothing. Or I can build an array of simplified primitives and have the DMA interrupt parse them and expand them into full instructions in real time (Line xyxy, line xyxy, spline abcdefgh, point xy, point xy... could even have macro 'sprites' commands... useful for text maybe?). The former is more straightforward, but the latter would help if I wanted to have obscured line removal for 3D objects.
adam
Baaaaaa!
Baaaaaa!
Posts: 4418
Joined: Sat Jun 24, 2006 9:15 pm 0

Re: Asteroids on Oscilloscope

Post by adam »

nice one - so would you like to open source it & have other people write games too?
User avatar
Kamineko
Baaaaaaa!
Baaaaaaa!
Posts: 7569
Joined: Fri Dec 17, 2004 2:53 pm 0
Contact:

Re: Asteroids on Oscilloscope

Post by Kamineko »

At the moment there's nothing to open source. There's about 1,500 lines of code here and 70% of that is the music player, which plays a format which I haven't fully defined yet. It's just a port of the Gravity Beam: Master Gaiden SMS player which is already MIT licensed. The rest is #defines and things that set up the GPIOs and modules of the dsPIC to 'act like an Ocelot'; sound pins, SPI DAC, pad reading, analogue battery status reading, blinky light. The most awesome secret hidden within the Ocelot code is the Mega Drive pad reading, and all it does is what it's supposed to.

I absolutely insist other people write games! But I'm not going to hang around and write and maintain libraries to help with that because I don't even know what I'm doing myself.

I haven't defined anything about the Ocelot as a physical 'platform' and I probably won't because I don't really have a reason to. What it is is what I have in front of me at any given moment. If I decide that I want to lop a 5V shift register on the side of this to interface with a YM2612 for Mega Drive sound, then that's what it'll become. Like an Arduino you buy off-the-shelf, what I have in front of me is highly-configurable Crazy Shit.

Until I define 'it', it doesn't make sense for anyone to try to writing games for it. You could write an emulator (or a JIT thing would be fine, considering what I say below about no RAM execution) for the dsPIC33 core, but you'd have to have silly things like emulating all the pins in software, or just take a random guess at what peripherals you're interested in using and high-level emulate those. The code is straightforward C code that compiles for dsPIC33 with Microchip's fork of gcc, so I'll probably write a quick SDL Ocelot runtime thing ("Notalot"?) just like the SDL visual runtime for the original Asteroids on Oscilloscope.

Without an emulator, you'd have to build your own gizmo to test anything as (what I think is) the whole point is to see your creations lit up in phosphors. If you're good enough to build a gizmo, you're probably the kind of person who would enjoy writing their own system code too. You'll probably have to to get the system running the way you want. Then you'll start adding whatever peripherals you think are appropriate to the task at hand (or whatever you can get your hands on, like me) and then you wouldn't have 'an Ocelot' you'd have an Adamlot.

I'm not against collaboration as an idea, but I don't think it's very practical. If you want to see an awesome vector game, go right ahead and make it yourself on whatever systems you've got to hand. :P Javascript if you've gotta. If you want to interface a red-hot super dual DAC with a MCU and make things happen, definitely do that too. If you absolutely desperately must see your own game on my Ocelot, then sure I suppose I could come up with some guidelines (ints are 16-bit, longs are 32-bit, use the most basic C you can, no malloc, here's a runtime stub and an interface to output video, music, sfx, etc.). But in the end I think it'll be more awesome if you've got your own system to see it on too!

If you want a better supported system (ie. an actual system that has any support :P) that uses a microcontroller to Do Game Stuff and will let other people play your awesome games then maybe you should have a look at Gamebuino.

Speaking of using other people's games, the board layout is absolutely packed now but it would be theoretically possible to have SD card FAT16 reading (Microchip have a lot of useful no-cost libraries for this which surprised me.) and therefore interchangeable games (But of course if I were selling complete SD card-based Ocelots: PATENTS zomg.). The dsPIC33 can't execute code from RAM (quite common in microcontrollers), so any cartridge-like interface would have to either load code into RAM and interpret it, or flash a reserved slot of flash PROM and jump to it. You'd probably have a stub executive bootloader-esque that lives permanently in the upper foobar Kbytes of PROM which also has some shared library routines in predefined locations that games can jump to.
User avatar
gumboot
Baaaaaaa!
Baaaaaaa!
Posts: 7652
Joined: Sun Aug 26, 2007 3:42 am 0
Location: trumpstain!

Re: Asteroids on Oscilloscope

Post by gumboot »

Let me know just as soon as you need a high-efficiency, high-quality 8-bit PRNG. I'm an expert!
adam
Baaaaaa!
Baaaaaa!
Posts: 4418
Joined: Sat Jun 24, 2006 9:15 pm 0

Re: Asteroids on Oscilloscope

Post by adam »

i was thinking more of open sourcing the design - publishing a schematic in other words - though seems a bit back of an envelope at this stage.
User avatar
Kamineko
Baaaaaaa!
Baaaaaaa!
Posts: 7569
Joined: Fri Dec 17, 2004 2:53 pm 0
Contact:

Re: Asteroids on Oscilloscope

Post by Kamineko »

Here's an important note.
The recommended solution goes a bit like this: you either have to set up a second DMA channel (wasting it) to write the phantom bits to DMA-accessible memory (wasting it), and have stub interrupt routines that do nothing but waste time...

Or you can simply manually read from the SPI buffer in the SPI interrupt you already have that fires every time you receive a word. You'll be servicing that interrupt every time you transmit a word anyway, so there's nothing to be wasted by using it to also acknowledge the word you 'received'. unsigned int x = SPI1BUF;, and every problem simply disappears. I like my solution. :3
Reading the SPI BUF manually only works up to certain clock rates, and then it will simply fail (probably due to timing meshing problems.) The DMA method works on all clock speeds.

I was getting blank screens on certain high clock rates, and I thought it was because I was going too fast for the DAC to handle. The DAC can receive very very super high clock rates, but I figured the 'framed SPI' of from the dsPIC was giving it bad sync pulses, because a 'sync pulse' might not be the required length for the DAC's 'hold CS high between messages' requirement. Turns out I was wrong about that, and you can chuck numbers at the DAC like a crazy thing and I'll eat 'em all up.

The more you know!

Speaking of which, two DMA channels to handle communication with the graphics hardware? That's X-TREME BLAST PROCESSING.
User avatar
Kamineko
Baaaaaaa!
Baaaaaaa!
Posts: 7569
Joined: Fri Dec 17, 2004 2:53 pm 0
Contact:

Re: Asteroids on Oscilloscope

Post by Kamineko »

You know that feeling when everything goes wrong and the solutions seem farther away then ever? When you've wasted all of your money and your time, with nothing to show for it? Everything around you crumbles and there's no hope left?

What's the opposite of that?

SPINNING CUBE, BITTTTTTTCHES.
User avatar
venusian
Baaaaaaa!
Baaaaaaa!
Posts: 6653
Joined: Wed Nov 06, 2002 2:22 pm 0
Location: South Gloucestershire

Re: Asteroids on Oscilloscope

Post by venusian »

Yay :D
User avatar
Kamineko
Baaaaaaa!
Baaaaaaa!
Posts: 7569
Joined: Fri Dec 17, 2004 2:53 pm 0
Contact:

Re: Asteroids on Oscilloscope

Post by Kamineko »

The great thing about making the Ocelot isn’t staying up at 3:30am writing the code for the vector character set, it’s showing everyone online that I did.
User avatar
Kamineko
Baaaaaaa!
Baaaaaaa!
Posts: 7569
Joined: Fri Dec 17, 2004 2:53 pm 0
Contact:

Re: Asteroids on Oscilloscope

Post by Kamineko »

I've written a converter that converts MIDI music files into 4 channel Ocelot music modules. It's working as far as I can see, so now I need a MIDI editor on the Linux side to edit the tunes. (On Windows, I use Cakewalk, but I'm not debugging the Ocelot on my Windows box because I take the device out and about.)

LMMS doesn't export midi. QTractor is weird.

Rosegarden looks the business. It takes a bit of lucky clicking to get it to output its tracks onto the MIDI channels you tell it to, but eventually it does what it's told. Now all I need to do is figure out how to get some audible output from Rosegarden. It should be as simple as...

Image

Audio in Linux, everybody.
User avatar
gumboot
Baaaaaaa!
Baaaaaaa!
Posts: 7652
Joined: Sun Aug 26, 2007 3:42 am 0
Location: trumpstain!

Re: Asteroids on Oscilloscope

Post by gumboot »

Couldn't you convert from something like fasttracker and run the editor in a dos emulator, or use one of those linux-native trackers?
User avatar
Kamineko
Baaaaaaa!
Baaaaaaa!
Posts: 7569
Joined: Fri Dec 17, 2004 2:53 pm 0
Contact:

Re: Asteroids on Oscilloscope

Post by Kamineko »

I could if I knew dick about fasttrackers format or wanted to use a DOS editor!

I used GoatTracker briefly for messing around with C64 stuff years ago. It was fun, but I wouldn't want to go anywhere near it again!
User avatar
Baggypants
Baaaaaaa!
Baaaaaaa!
Posts: 5328
Joined: Sun Mar 19, 2006 12:54 pm 0
Location: Salford(UK);
Contact:

Re: Asteroids on Oscilloscope

Post by Baggypants »

Kamineko wrote:I've written a converter that converts MIDI music files into 4 channel Ocelot music modules. It's working as far as I can see, so now I need a MIDI editor on the Linux side to edit the tunes. (On Windows, I use Cakewalk, but I'm not debugging the Ocelot on my Windows box because I take the device out and about.)

LMMS doesn't export midi. QTractor is weird.

Rosegarden looks the business. It takes a bit of lucky clicking to get it to output its tracks onto the MIDI channels you tell it to, but eventually it does what it's told. Now all I need to do is figure out how to get some audible output from Rosegarden. It should be as simple as...

Image

Audio in Linux, everybody.

Bah, Old FUD

It's much simpler now with PulseAudio!
Image


Of course I have PulseAudio running as a plugin for JACK. It's the only way to be sure.
"The eggs, get them!"
User avatar
Kamineko
Baaaaaaa!
Baaaaaaa!
Posts: 7569
Joined: Fri Dec 17, 2004 2:53 pm 0
Contact:

Re: Asteroids on Oscilloscope

Post by Kamineko »

User avatar
Kamineko
Baaaaaaa!
Baaaaaaa!
Posts: 7569
Joined: Fri Dec 17, 2004 2:53 pm 0
Contact:

Re: Asteroids on Oscilloscope

Post by Kamineko »

You think you've got it all figured out, ready to design and laser cut the wooden case and then it will be done, done, done!

And then somebody suggests 'Why don't you make the case out of green semi-transparent fluorescent-edged acrylic, in the shape of an asteroid?'

Smeg...!
Post Reply