Created
May 26, 2014 17:57
-
-
Save cryptix/2242cf8e15cd5e931255 to your computer and use it in GitHub Desktop.
Copying binary ints to a c programm using golangs encoding/binary
This file contains hidden or 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
package main | |
import ( | |
"bufio" | |
"encoding/binary" | |
"fmt" | |
"io" | |
"os" | |
"os/exec" | |
"runtime" | |
"time" | |
) | |
func main() { | |
fmt.Println("Starting Proc") | |
cmd := exec.Command("./sim") | |
stdin, err := cmd.StdinPipe() | |
checkFatal(err) | |
stderr, err := cmd.StderrPipe() | |
checkFatal(err) | |
go func() { | |
buffedErr := bufio.NewReader(stderr) | |
errLoop: | |
for { | |
line, err := buffedErr.ReadString('\n') | |
switch { | |
case err == nil: | |
fmt.Print(line) | |
case err == io.EOF: | |
fmt.Println("stdErr closed.") | |
break errLoop | |
default: | |
checkFatal(err) | |
} | |
} | |
}() | |
err = cmd.Start() | |
checkFatal(err) | |
var i int64 | |
for i = 8589934592; i <= 8589934592+32; i++ { | |
err = binary.Write(stdin, binary.LittleEndian, i) | |
checkFatal(err) | |
} | |
err = stdin.Close() | |
if err != nil { | |
logErr(err) | |
} | |
// sleep for a couple of cycles to let the cmd finish | |
time.Sleep(time.Millisecond * 100) | |
err = cmd.Wait() | |
checkFatal(err) | |
} | |
func checkFatal(err error) { | |
_, file, line, _ := runtime.Caller(1) | |
if err != nil { | |
fmt.Fprintf(os.Stderr, "Fatal from <%s:%d>\nError:%s", file, line, err) | |
os.Exit(1) | |
} | |
} | |
func logErr(err error) { | |
_, file, line, _ := runtime.Caller(1) | |
fmt.Fprintf(os.Stderr, "Error from <%s:%d>\nError:%s\n", file, line, err) | |
} |
This file contains hidden or 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
#include "de.tuhh.hbubert.firTester.c" | |
#include <stdio.h> | |
#include <unistd.h> | |
#define idxClk PSHDL_SIM_DE_TUHH_HBUBERT_FIRTESTER_CLK | |
#define idxRst PSHDL_SIM_DE_TUHH_HBUBERT_FIRTESTER_RST | |
#define idxInput PSHDL_SIM_DE_TUHH_HBUBERT_FIRTESTER_INPUT | |
#define idxVerify PSHDL_SIM_DE_TUHH_HBUBERT_FIRTESTER_VERIFY | |
#define idxTest PSHDL_SIM_DE_TUHH_HBUBERT_FIRTESTER_TEST | |
#define idxCAddr PSHDL_SIM_DE_TUHH_HBUBERT_FIRTESTER_CADDR | |
#define idxCValue PSHDL_SIM_DE_TUHH_HBUBERT_FIRTESTER_CVALUE | |
#define idxCWrite PSHDL_SIM_DE_TUHH_HBUBERT_FIRTESTER_CWRITE | |
#define idxDebug PSHDL_SIM_DE_TUHH_HBUBERT_FIRTESTER_FIRONE_COEFFS | |
#define bufSize 8 | |
union byteLongUnion | |
{ | |
uint8_t buf[bufSize]; | |
long number; | |
}; | |
void readCoeffs (const char* file_name) { | |
FILE* file = fopen (file_name, "r"); | |
long coeff = 0; | |
int addr = 0; | |
while (!feof (file)) { | |
// set address | |
pshdl_sim_setInput(idxCAddr, addr); | |
// clock one cycle | |
pshdl_sim_setInput(idxClk, 0); | |
pshdl_sim_run(); | |
pshdl_sim_setInput(idxClk, 1); | |
pshdl_sim_run(); | |
// set value | |
fscanf (file, "%ld", &coeff); | |
pshdl_sim_setInput(idxCValue, coeff); | |
// clock one cycle | |
pshdl_sim_setInput(idxClk, 0); | |
pshdl_sim_run(); | |
pshdl_sim_setInput(idxClk, 1); | |
pshdl_sim_run(); | |
// increase addr | |
addr++; | |
} | |
if (addr != 41) { | |
fprintf(stderr, "Error: not enough coefficients in %s\n", file_name); | |
exit(1); | |
} | |
fclose (file); | |
} | |
void update() { | |
pshdl_sim_setInput(PSHDL_SIM_DE_TUHH_HBUBERT_SETTEST_A, a); | |
pshdl_sim_setInput(PSHDL_SIM_DE_TUHH_HBUBERT_SETTEST_B, b); | |
pshdl_sim_setInput(idxClk, 0, 0); | |
pshdl_sim_run(); | |
pshdl_sim_setInput(idxClk, 1, 0); | |
pshdl_sim_run(); | |
c = pshdl_sim_getOutput(PSHDL_SIM_DE_TUHH_HBUBERT_SETTEST_C); | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
union byteLongUnion leBuf; | |
readCoeffs("coeffs.txt"); | |
int n = 0; | |
while(true) { | |
if (fread(leBuf.buf, bufSize, 1, stdin) != 1 || feof(stdin)) | |
{ | |
fprintf(stderr, "Exiting. n[%d] eof[%s]\n",n, feof(stdin) ? "true" : "false"); | |
break; | |
} | |
fprintf(stderr,"Read: %ld\n", leBuf.number); | |
n++; | |
} | |
// for(unsigned j = 32; j <= 42; ++j) { | |
// a=j; | |
// for(unsigned i = 1; i <= 10; ++i) { | |
// b=i; | |
// update(); | |
// printf("%ld*%ld=%ld\n",a,b, c); | |
// } | |
// } | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment