Christmas puzzle: SQI bit shuffler
For those of you who tend to get bored during the christmas holidays when the weather is rainy and all shops, bars, gyms etc. are closed I have a riddle to solve.
As input we have two longwords. The first contains an 8 bit command and a 16 bit address. The second contains 32 bits of data. They have to be transmitted over an SQI interface with four data lines SD0..3. The rules are:
- all bytes are transmitted with the most significant bit (MSb) first
- the address is in big-endian format (MSByte transmitted first)
- data is in little-edian format (LSB first)
- the P2 smart pins (P_SYNC_TX mode) transmit the LSb first
So if we write the input as 32 bit binary numbers with one character representing a binary digit we have:
don't care command address ; data ~~~~~~~~ XWVUTSRQ PONMLKJIHGFEDCBA; vutsrqpo nmlkjihg fedcba98 76543210
and we need the following output:
d0 = ~~~~~~~~~~~~~~~~ ~~ osgk8c04 AEIMQU d1 = ~~~~~~~~~~~~~~~~ ~~ pthl9d15 BFJNRV d2 = ~~~~~~~~~~~~~~~~ ~~ quimae26 CGKOSW d3 = ~~~~~~~~~~~~~~~~ ~~ rvjnbf37 DHLPTX
The challange is to find a sequence of P2 assembler instructions that calculates the valid output (4 registers d0..d3) from the input (registers cmdadr, data). The one who comes up with the shortest and most elegant solution wins a KISS board.
(Hint: I think I have a solution with 16 instructions)
Comments
I don't really need any more, but oh well
17 instructions, 34 cycles
I think I have a 14 instruction solution.
I need to verify that later....
(Edit: fixed register names)
yea, that seems slightly smarter
One additinal instruction (getbyte d3,data,#0) could be saved if data and d3 are the same register.
What annoys me a bit is that the SCLK pin has to be placed in the middle of the D1 and D2 pin so that all four can use the P_MINUS3_B to P_PLUS3_B input routing. That disables the use of ADDPINS to setup the four data pins at once and requires a lot more instructions.
This sort of thing is where you'd really want to use the streamer unit instead.
And the winner is... Ada (Wuerfel_21)
...sadly due to being the only participant.
Yes, probably. I thought using the streamer was too complicated and the bit shuffling has to be done anyway. But setting up the smart pins also takes a lot of instructions. I'll have to try both versions to see which one performs better.