/* UCOM interrupt EP_IN and EP_OUT endpoints handler */ static ErrorCode_t UCOM_int_hdlr(USBD_HANDLE_T hUsb, void *data, uint32_t event) { switch (event) { case USB_EVT_IN: /* USB_EVT_IN occurs when HW completes sending IN packet. So clear the busy flag for main loop to queue next packet. */ g_usb.usbTxFlags &= ~UCOM_TX_BUSY; if (RingBuffer_GetCount(&usb_txrb) >= 1) { g_usb.usbTxFlags |= UCOM_TX_BUSY; RingBuffer_Pop(&usb_txrb, g_usb.usbTx_buff); USBD_API->hw->WriteEP(g_usb.hUsb, HID_EP_IN, g_usb.usbTx_buff, AVAM_P_COUNT); } break; case USB_EVT_OUT: g_usb.usbRx_count = USBD_API->hw->ReadEP(hUsb, HID_EP_OUT, g_usb.usbRx_buff); #ifdef DEBUG_VERBOSE if (RingBuffer_GetCount(&usb_rxrb) == RX_BUF_CNT) { debug32("E:(%d-%x %x %x %x) usb_rxrb overflow evt out\n", g_usb.usbRx_count, g_usb.usbRx_buff[0], g_usb.usbRx_buff[1], g_usb.usbRx_buff[2], g_usb.usbRx_buff[3]); } #endif if (g_usb.usbRx_count >= AVAM_P_COUNT) { RingBuffer_Insert(&usb_rxrb, g_usb.usbRx_buff); g_usb.usbRx_count -= AVAM_P_COUNT; } if (g_usb.usbRxFlags & UCOM_RX_BUF_QUEUED) { g_usb.usbRxFlags &= ~UCOM_RX_BUF_QUEUED; if (g_usb.usbRx_count != 0) g_usb.usbRxFlags |= UCOM_RX_BUF_FULL; } break; case USB_EVT_OUT_NAK: /* queue free buffer for RX */ if ((g_usb.usbRxFlags & (UCOM_RX_BUF_FULL | UCOM_RX_BUF_QUEUED)) == 0) { g_usb.usbRx_count = USBD_API->hw->ReadReqEP(hUsb, HID_EP_OUT, g_usb.usbRx_buff, UCOM_RX_BUF_SZ); #ifdef DEBUG_VERBOSE if (RingBuffer_GetCount(&usb_rxrb) == RX_BUF_CNT) debug32("E: usb_rxrb overflow evt nak\n"); #endif if (g_usb.usbRx_count >= AVAM_P_COUNT) { RingBuffer_Insert(&usb_rxrb, g_usb.usbRx_buff); g_usb.usbRx_count -= AVAM_P_COUNT; } g_usb.usbRxFlags |= UCOM_RX_BUF_QUEUED; } break; default: break; } return LPC_OK; }
int test_nonce(struct mm_work *mw, uint32_t nonce2, uint32_t nonce) { /* Generate the work base on nonce2 */ struct work work; debug32("Test: %08x %08x\n", nonce2, nonce); miner_gen_nonce2_work(mw, nonce2, &work); /* Write the nonce to block header */ uint32_t *work_nonce = (uint32_t *)(work.header + 64 + 12); *work_nonce = bswap_32(nonce); /* Regen hash */ uint32_t *data32 = (uint32_t *)(work.header); unsigned char swap[80]; uint32_t *swap32 = (uint32_t *)swap; unsigned char hash1[32]; uint32_t *hash_32 = (uint32_t *)(hash1 + 28); flip80(swap32, data32); dsha256(swap, 80, hash1); if (*hash_32 != 0) return NONCE_HW; /* Compare hash with target */ return fulltest(hash1, mw->target); }
void miner_gen_nonce2_work(struct mm_work *mw, uint32_t nonce2, struct work *work) { uint8_t merkle_root[32], merkle_sha[64]; uint32_t *data32, *swap32, tmp32; int i; tmp32 = bswap_32(nonce2); memcpy(mw->coinbase + mw->nonce2_offset, (uint8_t *)(&tmp32), sizeof(uint32_t)); work->nonce2 = nonce2; gen_hash(mw->coinbase, merkle_root, mw->coinbase_len); memcpy(merkle_sha, merkle_root, 32); for (i = 0; i < mw->nmerkles; i++) { memcpy(merkle_sha + 32, mw->merkles[i], 32); gen_hash(merkle_sha, merkle_root, 64); memcpy(merkle_sha, merkle_root, 32); } data32 = (uint32_t *)merkle_sha; swap32 = (uint32_t *)merkle_root; flip32(swap32, data32); memcpy(mw->header + mw->merkle_offset, merkle_root, 32); debug32("D: Work nonce2: %08x\n", work->nonce2); calc_midstate(mw, work); uint8_t work_t[44]; #ifdef HW_PRE_CALC uint8_t work_t[] = { 0x05, 0x4e, 0x53, 0xc3, 0xc4, 0xd4, 0xba, 0x3e, 0x65, 0x40, 0x99, 0x4f, 0x06, 0x67, 0x91, 0x31, 0xa7, 0x2d, 0x66, 0xaa, 0x68, 0x4f, 0x0e, 0xdb, 0xc3, 0x6d, 0x95, 0x8a, 0x46, 0x6e, 0x4d, 0xb2, 0x1c, 0x26, 0x52, 0xfb, 0x52, 0xa0, 0x26, 0xf4, 0x19, 0x06, 0x12, 0x42}; #endif memcpy(work_t, work->data, 44); rev(work_t, 32); rev(work_t + 32, 12); memcpy(work->data, work_t, 44); #ifdef HW_PRE_CALC calc_prepare(work, work_t); memcpy((uint8_t *)(&tmp32), work->a1, 4); debug32("%08x,", tmp32); memcpy((uint8_t *)(&tmp32), work->a0, 4); debug32("%08x,", tmp32); memcpy((uint8_t *)(&tmp32), work->e2, 4); debug32("%08x,", tmp32); memcpy((uint8_t *)(&tmp32), work->e1, 4); debug32("%08x,", tmp32); memcpy((uint8_t *)(&tmp32), work->e0, 4); debug32("%08x,", tmp32); memcpy((uint8_t *)(&tmp32), work->a2, 4); debug32("%08x", tmp32); debug32("\n"); #endif calc_prepare1(work, work->data); memcpy((uint8_t *)(&tmp32), work->a1, 4); memcpy((uint8_t *)(&tmp32), work->a0, 4); memcpy((uint8_t *)(&tmp32), work->e2, 4); memcpy((uint8_t *)(&tmp32), work->e1, 4); memcpy((uint8_t *)(&tmp32), work->e0, 4); memcpy((uint8_t *)(&tmp32), work->a2, 4); }