C++に投稿されたコード一覧

C++ 文字列リテラルへのポインタを得るコマンド

// constptr || CONST_VALUE
int constptr( void** ppResult )	// この返値が *type_res に、*ppResult の値が reffunc の返値になるとする
{
	if ( *exinfo->npexflg & (EXFLG_1 | EXFLG_2) ) puterror( HSPERR_SYNTAX );
	static int stt_result;
	switch ( *type ) {
		case TYPE_STRING:
		case TYPE_DNUM:
			stt_result = reinterpret_cast<int>( &ctx->mem_mds[*val] );
			*ppResult = &stt_result;
			break;
		case TYPE_INUM:
			puterror( HSPERR_UNSUPPORTED_FUNCTION );	// CS中に埋め込まれているのでとれない
		default:
			puterror( HSPERR_SYNTAX );
	}
	code_next();
	if ( !(*type == TYPE_MARK && *val == 6) ) puterror( HSPERR_SYNTAX );	// or(||)
	code_next();
	return HSPVAR_FLAG_INT;
}

C++ Mapper019テスト

#include "Vmpu6502.h"
#include "NesRom.h"
#include "VMapper019.h"

int main(/*int argc, char **argv, char **env*/)
{
	Vmpu6502 *mpu = new Vmpu6502;

	// 外部要因
	mpu->din = 0x00;
	mpu->read_ack = 0;
	mpu->run = 0;
	mpu->mpu_reset = 0;
	mpu->NMI = 0;
	mpu->IRQn = 0;

	// リセット
	mpu->p_reset = 0;
	mpu->m_clock = 1;
	mpu->eval();
	mpu->p_reset = 1;
	mpu->m_clock = 0;
	mpu->eval();

	// NES rom
	NesRom rom;
	rom.Read("test.nes");

	VMapper019 map;

	map.n16kRomBanks = rom.n16kRomBanks;
	map.fMirroringType = rom.fMirroringType;

	// リセット
	map.p_reset = 0;
	map.m_clock = 1;
	map.eval();
	map.p_reset = 1;
	map.m_clock = 0;
	map.eval();

	// init
	map.init = 1;
	map.m_clock = 1;
	map.eval();
	map.init = 0;
	map.m_clock = 0;
	map.eval();

	FILE *fp;
	fp = fopen("log.txt", "w");
	setbuf(fp, NULL);

	int i;
	int vblank = 0;

	int f_2005_2006 = 0;
	int v_adrs_inc = 0;
	int v_adrs;

	unsigned char wram[0x800];
	memset(wram, 0, 0x800);
	unsigned char exram[0x2000];
	memset(exram, 0, 0x2000);

	int hcount = 0;

	for(i=0; i<500000; i++){

		mpu->m_clock = 1;
		mpu->eval();

		mpu->run = (i%3)==1;

		if(mpu->read_req){
			if(mpu->A<0x2000){
				mpu->din = wram[mpu->A&0x7FF];
			}
			else if(mpu->A&0x8000){
				map.prg_read = 1;
				map.ROM_SELn = 0;
				map.prg_A = mpu->A & 0x7FFF;

				map.m_clock = 1;
				map.eval();
				map.m_clock = 0;
				map.eval();

				map.prg_read = 0;

				mpu->din = rom.prg_rom[map.prg_ram_adrs];
			}
			else if(mpu->A>=0x6000){
				mpu->din = exram[mpu->A&0x1FFF];
			}
			else{
				switch(mpu->A){
				case 0x2002:
					mpu->din = (vblank<<7);
					vblank = 0;
					f_2005_2006 = 0;
					break;
				case 0x2007:
					printf("read 2007\n");
					break;
				case 0x4016:
				case 0x4017:
					mpu->din = 0;
					break;
				default:
					printf("no mem map : %04X\n", mpu->A);
				}
			}
		}

		static int t_ack = 0;
		mpu->read_ack = /*mpu->read_req;*/t_ack;
		t_ack = mpu->read_req;

		if(mpu->write_req){
			if(mpu->A<0x2000){
				wram[mpu->A&0x7FF] = mpu->dout;
			}
			else if(mpu->A&0x8000){

				map.prg_write = 1;
				map.ROM_SELn = 0;
				map.prg_A = mpu->A & 0x7FFF;
				map.prg_Din = mpu->dout;

				map.m_clock = 1;
				map.eval();
				map.m_clock = 0;
				map.eval();

				map.prg_write = 0;

				fprintf(fp, "CPU $%04X %02X  $%04X %02X  ",
					mpu->A, mpu->dout, map.prg_A, map.prg_Din
				);

				switch(mpu->A){
				case 0xE000: fprintf(fp, "bank4 %02X\n", map.v__DOT__prg_bank4);
					break;
				case 0xE800: fprintf(fp, "bank5 %02X\n", map.v__DOT__prg_bank5);
					break;
				case 0xF000: fprintf(fp, "bank6 %02X\n", map.v__DOT__prg_bank6);
					break;
				default: fprintf(fp, "\n");
				}

			}
			else if(mpu->A>=0x6000){
				exram[mpu->A&0x1FFF] = mpu->dout;
			}
			else{
				unsigned char data = mpu->dout;
				switch(mpu->A){
				case 0x2000:
					if(data & 0x04) v_adrs_inc = 32;
					else			v_adrs_inc = 1;
					break;
				case 0x2005:
					f_2005_2006 = !f_2005_2006;
					break;
				case 0x2006:
					f_2005_2006 = !f_2005_2006;
					if(f_2005_2006){	// 上位8bit first write
						v_adrs = data;
					}
					else{	// 下位8bit second write
						v_adrs = (v_adrs<<8) | data;
					}
					break;
				case 0x2007:
				//	fprintf(fp, "VW %04X:%02X\n", v_adrs, data);
					v_adrs += v_adrs_inc;
					break;
				}
			}

		}

		mpu->m_clock = 0;
		mpu->eval();

		mpu->NMI = 0;
		mpu->IRQn = 1;

		static int old_op = 0;
		if(mpu->v__DOT__ir != old_op){
			old_op = mpu->v__DOT__ir;

			static int base = 0;
			base++;
			if(base>0){
			fprintf(fp,"%04X %02X : %02X %02X %02X %02X %d%d%d%d%d%d%d\n",
				mpu->v__DOT__pc-1, mpu->v__DOT__ir, mpu->v__DOT__ra, mpu->v__DOT__rx, mpu->v__DOT__ry, mpu->v__DOT__sp,
				mpu->v__DOT__fn, mpu->v__DOT__fv, mpu->v__DOT__fb, mpu->v__DOT__fd,
				mpu->v__DOT__fi, mpu->v__DOT__fz, mpu->v__DOT__fc
			);
			}

			static int pl = 0;
			pl++;
			if(pl==7828 || pl==16336 || pl==24846 || pl==33356 || pl==41866){
				vblank = 1;
			}
		}
	}

	fclose(fp);
	return 0;
}

C++ SystemC で RAM, ROM もどき

#include <systemc.h>

template <typename datarange, typename addressrange>
class SimROM : public sc_module
{
protected:
	datarange *const m_data;
	const int m_size;
	sc_in<bool> w_reset, w_clock;
	sc_in<addressrange> w_a;
	sc_out<datarange> w_q;

	void qout(void)
	{
		uint32_t a = w_a.read();
		assert(a < m_size);
		w_q.write(m_data[a]);
	}
	virtual void thread()
	{
		while(true){
			wait(w_clock.posedge_event());
			qout();
		}
	}
public:
	SC_HAS_PROCESS(SimROM);
	void Assign(sc_signal<bool> reset, sc_signal<bool> clock, sc_signal<addressrange> a, sc_signal<datarange> q)
	{
		w_reset(reset);
		w_clock(clock);
		w_a(a);
		w_q(q);
	}
	SimROM(sc_module_name n, int size)
		: sc_module(n), m_data(new datarange [size]), m_size(size),
		w_reset("p_reset"), w_clock("m_clock"),
		w_a("a"), w_q("q")
	{
		SC_THREAD(thread);
	}
	virtual ~SimROM()
	{
		delete [] m_data;
	}
};

template <typename D, typename A>
class SimRAM : public SimROM <D, A>
{
protected:
	sc_in<D> w_d;
	sc_in<bool> w_we;

	void thread(void)
	{
		while(true){
			wait(SimROM<D, A>::w_clock.posedge_event());
			SimROM<D, A>::qout();
			if(w_we.read() == 1){
				SimROM<D, A>::m_data[SimROM<D, A>::w_a.read()] = w_d.read();
			}
		}
	}
public:
	SC_HAS_PROCESS(SimRAM);
	SimRAM(sc_module_name n, int size)
	 : SimROM<D, A>::SimROM(n, size), w_d("d"), w_we("we")
	{
	}
};

C++ DDSパラメータを求める

/*
 DDSで使用する加算値としきい値を求めるプログラム

	clk_in : 入力クロック
	max : しきい値
	add : 加算値
	count : カウンタ
	clk_out : 出力クロック

	if(clk_in){
		if(count>max){
			count -= max;
			clk_out();
		}
		else{
			count += add;
		}
	}
*/

#include<stdio.h>
#include<math.h>

int main(void)
{
	const int base = 50000000; // ベースクロック
	const double goal = 1789772.5; // 生成する周波数

	int hz, count, add, max;
	int amari;

	int div = (int)(base / goal + 0.5);

	int best_add, best_max;
	double best_gosa = base;
	for(add=1; add<100; add++){

		double gosa_min;
		gosa_min = 0x7FFFFFFF;

		int hosei = -add;

		for(;;){
			max = div * add + hosei;

			hz = 0;
			count = 0;
			amari = 0;
			for(int i=0; i<base; i++){
				if(count>max){
					count -= max;
					amari += count;
					hz++;
				}
				else{
					count += add;
				}
			}

			double Hz;
			Hz = hz + (double)count / max;

			double p;
			p = Hz - goal;

			if(fabs(p) < gosa_min){
				printf("加算値 %3d しきい値 %6d 補正 %3d  誤差 %f\n", add, max, hosei, p);
				gosa_min = fabs(p);
				if(gosa_min < best_gosa){
					best_add = add;
					best_max = max;
					best_gosa = gosa_min;
					printf("更新 誤差 %f\n", gosa_min);
				}
			}
			else break;

			if(p>0){
				hosei++;
			}
			else{
				hosei--;
			}
		}
	}

	printf("加算値 %d  しきい値 %d  誤差 %f\n", best_add, best_max, best_gosa);

	return 0;
}

C++ モノクロ

// write SFL
// 下位4bitを0にしてモノクロのパレットを選択

			if(f_col_mono) VIDEO = pal_ram.dout<5:4> || 0x0;
			else VIDEO = pal_ram.dout<5:0>;
			PPU_VIDEO_enable();

C++ verilog named block

`default_nettype none

module hoge;
	reg clock;
	initial begin
		clock = 1'b0;

		forever begin
			#4 clock <= ~clock;
		end
	end

	always @(posedge clock) begin: increment //label を付けると
		reg [3:0] count; //局所配線宣言が可能
		//always の中で wire 宣言はエラーになる
		//wire [2:0] dd = {clock, clock, clock}; 

		//parameter は宣言可能
		parameter SEQ_INIT = 4'd0;
		parameter SEQ_II = 4'd1;
		if(count === 4'hx) begin
			count <= 4'h0;
		end else begin
			count <= count + 4'd1;
		end
	end

	//これはスコープを抜けるのでエラーになる。
	//wire [3:0] decrement = ~count;
	//label.regname とすると使えるようだ
	wire [3:0] decrement = ~increment.count;
	wire [3:0] ss = increment.SEQ_II;

	begin: nanmo //単なる begin - end では局所 wire 宣言が利用可能
		wire [7:0] dd = {decrement, ss};
		//parameter はどちらでも使えるようだ。
		parameter XOR = 8'hee;
	end

	wire [7:0] rr = nanmo.dd ^ nanmo.XOR;
endmodule

C++ Tabの読み飛ばし?

#include<stdio.h>

int main(void)
{
	FILE *fp;
	fp = fopen("sample.txt", "r");

	char buf[101];

	while(fgets(buf, 100, fp)){
		int i, j;
		char p[10];
		sscanf(buf, "%d\t%[^\t]\t%d\n", &i, p, &j); // ←間違ってる?
		printf("%d,%s,%d\n", i, p, j);
	}

	fclose(fp);
	return 0;
}

sample.txt ----------
2	BB	3
1		4
---------------------

output---------------
2,BB,3
1,4
,3
---------------------

C++ DE0のFT245向けD2XXドライバ使用送信テスト

#include<windows.h>

#include<stdio.h>
#include<time.h>

#include "ftd2xx.h"
#pragma comment(lib, "ftd2xx.lib")

int main(void)
{
	char dbuf[64];
	FT_ListDevices(0, dbuf, FT_LIST_BY_INDEX | FT_OPEN_BY_DESCRIPTION);
	printf("%s\n", dbuf);

	FT_STATUS ftStatus;
	FT_HANDLE ftHandle;

	ftStatus = FT_OpenEx(dbuf, FT_OPEN_BY_DESCRIPTION, &ftHandle);
	if(ftStatus!=FT_OK){
		printf("Failed to open port\n");
		return 0;
	}

	FT_ResetDevice(ftHandle);
	FT_Purge(ftHandle, FT_PURGE_RX | FT_PURGE_TX);

	FT_SetTimeouts(ftHandle, NULL, NULL);

	ftStatus = FT_SetBaudRate(ftHandle, FT_BAUD_921600);
	if(ftStatus!=FT_OK){
		printf("Failed to set BaudRate\n");
	}

	ftStatus = FT_SetBitMode(ftHandle, 0xFF/*1=out 0=in*/, 1/*bit-bang mode on*/);
	if(ftStatus!=FT_OK){
		printf("Failed to set bit mode\n");
	}

	int i;
	int total = 0;
	const int size = 4*1024*1024;
	unsigned char *buf = unsigned char[size];
	DWORD send_size;
	FT_STATUS ftTxStatus;

clock_t st, et;
st = clock();

//	for(i=0; i<1; i++){
		ftTxStatus = FT_Write(ftHandle, buf, size, &send_size);
		if(ftTxStatus!=FT_OK){
			printf("send err\n");
		}
		if(send_size!=size){
			printf("send err\n");
		}
		total += size;
//	}

et = clock();
clock_t u = et - st;
double s = u / 1000.0;
printf("%0.1f KB/s\n", total/s/1024);

	delete[] buf;
	FT_Close(ftHandle);

	return 0;
}

C++ DE1のFT245向けD2XXドライバ使用通信テスト

#include<windows.h>

#include<stdio.h>
#include<time.h>

#include "ftd2xx.h"
#pragma comment(lib, "ftd2xx.lib")

int main(void)
{
	char dbuf[64];
	FT_ListDevices(0, dbuf, FT_LIST_BY_INDEX | FT_OPEN_BY_DESCRIPTION);
	printf("%s\n", dbuf);

	FT_STATUS ftStatus;
	FT_HANDLE ftHandle;

	ftStatus = FT_OpenEx(dbuf, FT_OPEN_BY_DESCRIPTION, &ftHandle);
	if(ftStatus!=FT_OK){
		printf("Failed to open port\n");
		return 0;
	}

	FT_ResetDevice(ftHandle);
	FT_Purge(ftHandle, FT_PURGE_RX | FT_PURGE_TX);

	FT_SetTimeouts(ftHandle, NULL, NULL);

	ftStatus = FT_SetBaudRate(ftHandle, FT_BAUD_921600);
	if(ftStatus!=FT_OK){
		printf("Failed to set BaudRate\n");
	}

	ftStatus = FT_SetBitMode(ftHandle, 0xFF/*1=out 0=in*/, 1/*bit-bang mode on*/);
	if(ftStatus!=FT_OK){
		printf("Failed to set bit mode\n");
	}

	int i;
	int total = 0;
	const int size = 62*16;
	unsigned char buf[size];
	DWORD send_size;
	FT_STATUS ftTxStatus;

	for(i=0; i<size; i++) buf[i] = rand();
	buf[size-1] = 0;

clock_t st, et;
st = clock();

	for(i=0; i<10; i++){
		ftTxStatus = FT_Write(ftHandle, buf, size, &send_size);
		if(ftTxStatus!=FT_OK){
			printf("send err\n");
		}
		if(send_size!=size){
			printf("send err\n");
		}
		total += size;
	}

et = clock();
clock_t u = et - st;
double s = u / 1000.0;
printf("%0.1f Kbps\n", total/s/1024);

	FT_Close(ftHandle);

	return 0;
}

C++ Boost::Spirit::QiによるWavefront Objファイルのパーサ

#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/home/phoenix.hpp>
#include <fstream>

#include "Vector2d.h"
#include "Vector3d.h"

BOOST_FUSION_ADAPT_STRUCT(
	float3d,
	(float, x)
	(float, y)
	(float, z)
);

BOOST_FUSION_ADAPT_STRUCT(
	float2d,
	(float, x)
	(float, y)
);

class ObjFile {
public:
	class Group {
	public:
		std::string name;
		std::string material;
		std::vector<float3d> vertexList;
		std::vector<float3d> normalList;
		std::vector<float2d> texCoordList;
	};

	class Index {
	public:
		u32 vertex, texcoord, normal;
	};

	class Face {
	public:
		Index i1, i2, i3;
	};

	std::string materialFile;
	std::vector<Group> groups;
};

BOOST_FUSION_ADAPT_STRUCT(
	ObjFile,
	(std::string, materialFile)
	(std::vector<ObjFile::Group>, groups)
);

BOOST_FUSION_ADAPT_STRUCT(
	ObjFile::Group,
	(std::string, name)
	(std::string, material)
	(std::vector<float3d>, vertexList)
	(std::vector<float3d>, normalList)
	(std::vector<float2d>, texCoordList)
	(std::vector<ObjFile::Face>, faceList)
);

BOOST_FUSION_ADAPT_STRUCT(
	ObjFile::Index,
	(u32, vertex)
	(u32, texcoord)
	(u32, normal)
);

BOOST_FUSION_ADAPT_STRUCT(
	ObjFile::Face,
	(ObjFile::Index, i1)
	(ObjFile::Index, i2)
	(ObjFile::Index, i3)
);

using namespace boost::spirit;

template <typename Iterator>
class ObjParser : public qi::grammar<Iterator, ObjFile()>
{
	qi::rule<Iterator, float3d()> float3d_;
	qi::rule<Iterator, float2d()> float2d_;
	qi::rule<Iterator, ObjFile::Index()> index;

	qi::rule<Iterator, float3d()> vert_line;
	qi::rule<Iterator, float3d()> normal_line;
	qi::rule<Iterator, float2d()> texcoord_line;
	qi::rule<Iterator, std::string()> group_line;
	qi::rule<Iterator, std::string()> usemtl_line;
	qi::rule<Iterator, std::string()> mtllib_line;
	qi::rule<Iterator, ObjFile::Face()> face_line;
	qi::rule<Iterator> surface_line;
	qi::rule<Iterator> comment_line;

	qi::rule<Iterator, ObjFile::Group()> group;

	qi::rule<Iterator, ObjFile()> start;

public:
	ObjParser()
		: ObjParser::base_type(start)
	{
		using namespace qi;
		namespace phx = boost::phoenix;

		float3d_ %= float_ >> ' ' >> float_ >> ' ' >> float_;
		float2d_ %= float_ >> ' ' >> float_;
		index = eps[_val = phx::construct<ObjFile::Index>()]
				>> uint_[phx::at_c<0>(_val) = qi::_1]
				>> !('/' >> uint_[phx::at_c<1>(_val) = qi::_1]
					>> !('/' >> uint_[phx::at_c<2>(_val) = qi::_1])
				);

		vert_line %= "v " >> float3d_ >> eol;
		normal_line %= "vn " >> float3d_ >> eol;
		texcoord_line %= "vt " >> float2d_ >> eol;

		group_line %= "g " >> +(char_ - eol) >> eol;

		face_line %= "f " >> index >> ' ' >> index >> ' ' >> index >> eol;
		surface_line = "s " >> *(char_ - eol) >> eol;

		usemtl_line %= "usemtl " >> +(char_ - eol) >> eol;
		mtllib_line %= "mtllib " >> +(char_ - eol) >> eol;

		comment_line = '#' >> *(char_ - eol) >> eol | eol;

		group = eps[_val = phx::construct<ObjFile::Group>()]
				>> group_line[phx::at_c<0>(_val) = qi::_1]
				>> *(
						usemtl_line[phx::at_c<1>(_val) = qi::_1]
						| vert_line[phx::push_back(phx::at_c<2>(_val), qi::_1)]
						| normal_line[phx::push_back(phx::at_c<3>(_val), qi::_1)]
						| texcoord_line[phx::push_back(phx::at_c<4>(_val), qi::_1)]
						| comment_line
				);

		start = eps[_val = phx::construct<ObjFile>()]
				>> *(
					group[phx::push_back(phx::at_c<1>(_val), qi::_1)]
					| mtllib_line[phx::at_c<0>(_val) = qi::_1]
					| comment_line
				);
	}
};

int main(int argc, const char** argv) {
	if (argc == 0) {
		std::cout << "require one argument : .obj filepath" << std::endl;
		return -1;
	}

	std::ifstream file(argv[0]);
	std::string source;
	for(char c; !file.eof() && file.get(c); ) {
		source.push_back(c);
	}
	file.close();

	ObjFile value;
	ObjParser<std::string::iterator> parser;
	qi::parse(source.begin(), source.end(), parser, value);

	return 0;
}
Total Pages: 2 / 41234

よく投稿されているコード

タグ

最近投稿されたコード