+ if (f->bits >= 31)
+ return; /* Overflow, won't happen */
+
+ f->data |= (bit << f->bits);
+ f->bits++;
+}
+
+static void frame_clean(struct legic_frame * const f)
+{
+ f->data = 0;
+ f->bits = 0;
+}
+
+static uint32_t perform_setup_phase_rwd(int iv)
+{
+
+ /* Switch on carrier and let the tag charge for 1ms */
+ AT91C_BASE_PIOA->PIO_SODR = GPIO_SSC_DOUT;
+ SpinDelay(1);
+
+ legic_prng_init(0); /* no keystream yet */
+ frame_send_rwd(iv, 7);
+ legic_prng_init(iv);
+
+ frame_clean(¤t_frame);
+ frame_receive_rwd(¤t_frame, 6, 1);
+ legic_prng_forward(1); /* we wait anyways */
+ while(timer->TC_CV < 387) ; /* ~ 258us */
+ frame_send_rwd(0x19, 6);
+
+ return current_frame.data;
+}
+
+static void LegicCommonInit(void) {
+ FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
+ SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
+ FpgaSetupSsc();
+ FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_TX);
+
+ /* Bitbang the transmitter */
+ AT91C_BASE_PIOA->PIO_CODR = GPIO_SSC_DOUT;
+ AT91C_BASE_PIOA->PIO_OER = GPIO_SSC_DOUT;
+ AT91C_BASE_PIOA->PIO_PER = GPIO_SSC_DOUT;
+
+ setup_timer();
+
+ crc_init(&legic_crc, 4, 0x19 >> 1, 0x5, 0);
+}
+
+/* Switch off carrier, make sure tag is reset */
+static void switch_off_tag_rwd(void)
+{
+ AT91C_BASE_PIOA->PIO_CODR = GPIO_SSC_DOUT;
+ SpinDelay(10);
+ WDT_HIT();
+}
+/* calculate crc for a legic command */
+static int LegicCRC(int byte_index, int value, int cmd_sz) {
+ crc_clear(&legic_crc);
+ crc_update(&legic_crc, 1, 1); /* CMD_READ */
+ crc_update(&legic_crc, byte_index, cmd_sz-1);
+ crc_update(&legic_crc, value, 8);
+ return crc_finish(&legic_crc);
+}
+
+int legic_read_byte(int byte_index, int cmd_sz) {
+ int byte;
+
+ legic_prng_forward(4); /* we wait anyways */
+ while(timer->TC_CV < 387) ; /* ~ 258us + 100us*delay */
+
+ frame_send_rwd(1 | (byte_index << 1), cmd_sz);
+ frame_clean(¤t_frame);
+
+ frame_receive_rwd(¤t_frame, 12, 1);
+
+ byte = current_frame.data & 0xff;
+
+ if( LegicCRC(byte_index, byte, cmd_sz) != (current_frame.data >> 8) ) {
+ Dbprintf("!!! crc mismatch: expected %x but got %x !!!",
+ LegicCRC(byte_index, current_frame.data & 0xff, cmd_sz),
+ current_frame.data >> 8);
+ return -1;
+ }
+
+ return byte;
+}
+
+/* legic_write_byte() is not included, however it's trivial to implement
+ * and here are some hints on what remains to be done:
+ *
+ * * assemble a write_cmd_frame with crc and send it
+ * * wait until the tag sends back an ACK ('1' bit unencrypted)
+ * * forward the prng based on the timing
+ */
+//int legic_write_byte(int byte, int addr, int addr_sz, int PrngCorrection) {
+int legic_write_byte(int byte, int addr, int addr_sz) {
+ //do not write UID, CRC
+ if(addr <= 0x04) {
+ return 0;
+ }
+ //== send write command ==============================
+ crc_clear(&legic_crc);
+ crc_update(&legic_crc, 0, 1); /* CMD_WRITE */
+ crc_update(&legic_crc, addr, addr_sz);
+ crc_update(&legic_crc, byte, 8);
+
+ uint32_t crc = crc_finish(&legic_crc);
+ uint32_t cmd = ((crc <<(addr_sz+1+8)) //CRC
+ |(byte <<(addr_sz+1)) //Data
+ |(addr <<1) //Address
+ |(0x00 <<0)); //CMD = W
+ uint32_t cmd_sz = addr_sz+1+8+4; //crc+data+cmd
+
+ legic_prng_forward(4); /* we wait anyways */
+ while(timer->TC_CV < 387) ; /* ~ 258us */
+ frame_send_rwd(cmd, cmd_sz);
+
+ AT91C_BASE_PIOA->PIO_ODR = GPIO_SSC_DIN;
+ AT91C_BASE_PIOA->PIO_PER = GPIO_SSC_DIN;
+
+ //== wait for ack ====================================
+ int t, old_level=0, edges=0;
+ int next_bit_at =0;
+ while(timer->TC_CV < 387) ; /* ~ 258us */
+ for(t=0; t<80; t++) {
+ edges = 0;
+ next_bit_at += TAG_TIME_BIT;
+ while(timer->TC_CV < next_bit_at) {
+ int level = (AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_DIN);
+ if(level != old_level) {
+ edges++;
+ }
+ old_level = level;
+ }
+ if(edges > 20 && edges < 60) { /* expected are 42 edges */
+ int t = timer->TC_CV;
+ int c = t/TAG_TIME_BIT;
+ timer->TC_CCR = AT91C_TC_SWTRG;
+ while(timer->TC_CV > 1) ; /* Wait till the clock has reset */
+ legic_prng_forward(c-1);
+ return 0;
+ }
+ }
+ timer->TC_CCR = AT91C_TC_SWTRG;
+ while(timer->TC_CV > 1) ; /* Wait till the clock has reset */
+ return -1;
+}
+
+int LegicRfReader(int offset, int bytes) {
+
+ // ice_legic_setup();
+ // ice_legic_select_card();
+ // return 0;