Custom Logic Tutorial - Writing the Code
Frozen Content
Our design requires two custom logic circuits:
- A clock pulse circuit – which takes as input a control signal from the Custom Instrument component. The circuit needs to generate a 1 clock cycle pulse (High) when this input control signal goes High.
- An accumulation circuit – which is enabled by the output of the clock pulse circuit being driven High. The accumulation circuit takes as input a new value specified on, and output by, the Custom Instrument component. It adds this new value to its internally-stored total and outputs the new accumulated total to the subsequent latch (D-type Flip-Flop).
Let's create the C source code for each of these required circuits, in two separate source files.
Code for the Clock Pulse Circuit
- Right-click on the
CHC_Accumulator.PrjFpg
entry in the Projects panel and choose Add New to Project » C File. A new C source document is created (Source1.C
) and opened as the active document in the main design window. Save this document with the namePulseGenerator.C
, in the same location as the project file.
All source C (and header) files containing functions to be exported must be added to the FPGA project. This allows the C-to-Hardware Compiler to find the files for subsequent HDL generation.
- Type the following C code into the document (comments are optional but are always good to add for readability!):
/*************************************************************************
|*
|* Function : GeneratePulse
|*
|* Parameters : GO_I, GO_PULSE_O
|*
|* Returns : none
|*
|* Description : Generates a 1 clock cycle pulse when GO_I transitions
|* from Low to High
*/
#include <stdbool.h>
static bool s_GoPrev = false;
void GeneratePulse(bool GO_I, bool* GO_PULSE_O)
{
*GO_PULSE_O = false;
if (GO_I && (s_GoPrev == false))
*GO_PULSE_O = true;
s_GoPrev = GO_I;
}
- Save the document.
Code for the Accumulator Circuit
- Right-click on the
CHC_Accumulator.PrjFpg
entry in the Projects panel and choose Add New to Project » C File. A new C source document is created (Source1.C
) and opened as the active document in the main design window. Save this document with the nameAccumulator.C
, in the same location as the project file.
- Type the following C code into the document:
/********************************************************************
|*
|* Function : Accumulate
|*
|* Parameters : VALUE_I, TOTAL_O
|*
|* Returns : none
|*
|* Description : Adds VALUE_I to the internal total. The total is
|* placed in TOTAL_O.
*/
#include <stdint.h>
static uint32_t s_Total = 0;
void Accumulate(uint32_t VALUE_I, uint32_t* TOTAL_O)
{
s_Total += VALUE_I;
*TOTAL_O = s_Total;
}
- Save the document.