forked from KastnerRG/pp4fpgas
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update cordic code to include testbench
- Loading branch information
1 parent
a9bf74f
commit 9005224
Showing
4 changed files
with
105 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
This is traditional CORDIC computation of sine and cosine. | ||
The current code is based on [FXT: cordic-circ-demo.cc] | ||
Correctly calculates cos and sine between 0-90 degrees (0-100). | ||
INPUT: | ||
double theta: Input angle | ||
long n: Number of iterations. | ||
OUTPUT: | ||
double &s: Reference to the sine part | ||
double &c: Reference to the cos part | ||
error_sin= [abs(s-zs)/zs]*100; | ||
error_cos= [abs(c-zc)/zc]*100; | ||
Total_Error_Sin = sum(error_sin) | ||
Total_error_Cos = sum(error_cos) | ||
*/ | ||
#include <math.h> | ||
#include"cordic.h" | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
using namespace std; | ||
//#define M_PI 3.1415926536897932384626 | ||
|
||
double abs_double(double var){ | ||
if ( var < 0) | ||
var = -var; | ||
return var; | ||
|
||
|
||
} | ||
int main(int argc, char **argv) | ||
{ | ||
|
||
|
||
FILE *fp; | ||
|
||
UINTYPE_12 n = 32; | ||
cos_sin_TYPE s; //sine | ||
cos_sin_TYPE c; //cos | ||
theta_TYPE radian; //radian versuin of degree | ||
|
||
//zs=sin, zc=cos using math.h in VivadoHLS | ||
double zs, zc; // sine and cos values calculated from math. | ||
|
||
//Error checking | ||
double Total_Error_Sin=0.0; | ||
double Total_error_Cos=0.0; | ||
double error_sin=0.0, error_cos=0.0; | ||
|
||
fp=fopen("out.dat","w"); | ||
for(int i=1;i<NUM_DEGREE;i++) { | ||
radian = i*M_PI/180; | ||
cordic(radian, s, c, n); | ||
zs= sin((double)radian); | ||
zc = cos((double)radian); | ||
error_sin=(abs_double((double)s-zs)/zs)*100.0; | ||
error_cos=(abs_double((double)c-zc)/zc)*100.0; | ||
Total_Error_Sin=Total_Error_Sin+error_sin; | ||
Total_error_Cos=Total_error_Cos+error_cos; | ||
|
||
fprintf(fp, "degree=%d, radian=%f, cos=%f, sin=%f\n", i, (double)radian, (double)c, (double)s); | ||
} | ||
|
||
fclose(fp); | ||
|
||
printf ("Total_Error_Sin=%f, Total_error_Cos=%f, \n", Total_Error_Sin, Total_error_Cos); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
|
||
#ifndef CORDIC_H | ||
#define CORDIC_H | ||
#include "ap_fixed.h" | ||
|
||
typedef unsigned int UINTYPE_12; | ||
typedef ap_fixed<12,2> THETA_TYPE; | ||
typedef ap_fixed<12,2> COS_SIN_TYPE; | ||
|
||
const int NUM_DEGREE=90; | ||
static THETA_TYPE cordic_ctab[64]={0.78539816339744828000,0.46364760900080609000,0.24497866312686414000,0.12435499454676144000,0.06241880999595735000,0.03123983343026827700,0.01562372862047683100,0.00781234106010111110,0.00390623013196697180,0.00195312251647881880,0.00097656218955931946,0.00048828121119489829,0.00024414062014936177,0.00012207031189367021,0.00006103515617420877,0.00003051757811552610,0.00001525878906131576,0.00000762939453110197,0.00000381469726560650,0.00000190734863281019,0.00000095367431640596,0.00000047683715820309,0.00000023841857910156,0.00000011920928955078,0.00000005960464477539,0.00000002980232238770,0.00000001490116119385,0.00000000745058059692,0.00000000372529029846,0.00000000186264514923,0.00000000093132257462,0.00000000046566128731,0.00000000023283064365,0.00000000011641532183,0.00000000005820766091,0.00000000002910383046,0.00000000001455191523,0.00000000000727595761,0.00000000000363797881,0.00000000000181898940,0.00000000000090949470,0.00000000000045474735,0.00000000000022737368,0.00000000000011368684,0.00000000000005684342,0.00000000000002842171,0.00000000000001421085,0.00000000000000710543,0.00000000000000355271,0.00000000000000177636,0.00000000000000088818,0.00000000000000044409,0.00000000000000022204,0.00000000000000011102,0.00000000000000005551,0.00000000000000002776,0.00000000000000001388,0.00000000000000000694,0.00000000000000000347,0.00000000000000000173,0.00000000000000000087,0.00000000000000000043,0.00000000000000000022,0.00000000000000000011}; | ||
|
||
void cordic(THETA_TYPE theta, COS_SIN_TYPE &s, COS_SIN_TYPE &c) | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
############################################################ | ||
## This file is generated automatically by Vivado HLS. | ||
## Please DO NOT edit it. | ||
## Copyright (C) 2012 Xilinx Inc. All rights reserved. | ||
############################################################ | ||
open_project hls_cordic | ||
set_top cordic_circ | ||
add_files cordic.h | ||
add_files cordic.cpp | ||
add_files -tb out.gold.dat | ||
add_files -tb cordic_test.cpp | ||
open_solution "solution1" | ||
set_part {xc7z020clg484-1} | ||
create_clock -period 10 | ||
|
||
source "./directives.tcl" | ||
csynth_design |