樹莓派 HATs ︰ I2C 拾遺《三》

人們通常希望『好人作到底,送佛送到西』,只不過有時候

MAMELogoTM
Multiple Arcade Machine Emulator

220px-Mspacman_and_galaga_act_like_israel_and_palestine
街頭遊戲機

一九九七年,義大利程式設計師 Nicola Salmoria 開啟了街頭電玩的軟體模擬器發展,當年一月二十九日發表的 DOS 平台的 Multi-Pac v0.8 版, 就是現今著名 MAME 的前身。據知 MAME 的目標在於盡可能的忠實而且精確的模擬各種街機遊戲,它的宗旨為著保存遊戲的歷史,進而避免古老的遊戲恐將遺失或被忘卻。計畫全稱是個縮首字,由 Multiple Arcade Machine Emulator 而得名。

就像 WiKi  上所講的, Philosophy and accuracy 】︰

The stated aim of the project is to document hardware, and so MAME takes a somewhat purist 【純正化】view of emulation, prohibiting 【禁止】programming hacks that might make a game run improperly or run faster at the expense of emulation accuracy. Components such as CPUs are emulated at a low level (meaning individual instructions are emulated) whenever possible, and high-level emulation (HLE) is only used when a chip is completely undocumented and cannot be reverse-engineered 【逆向工程】 in detail. Signal level emulation is used to emulate audio circuitry that consists of analog components.

這大概不只是『 bit by bit 』的『拷貝』而已,或許真的是『克隆』的吧!然而至今『硬體文件』依然付之闕如,這事二零零八年計畫督導者是這麼說的︰

We want to document the hardware. Now a lot of people will say; “Where’s your document? You just write a bunch of source code.” And yes, that’s true. One thing I’ve learned is that keeping documentation synced with source code is nearly impossible. The best proof that your documentation is right is “does this code work“.

Aaron Giles, California Extreme 2008

果真是『大哉辯』的啊!『能用』比之於『能文』到底哪個重要的呢?希望不會有一天又要『再詮釋』一次的吧!!??這就宛如在《 Raspberryλ◇樹莓λ 》一文中的『 cgroup 』是什麼的呢?就像《 kernel lacks cgroups or memory controller not avaiable, not … 》裡所議論的一樣,即使最近的文件也就如《 Web browser beta 》所講的『差不多』罷了,也許是不容易『說清楚』的吧。由於 MAME 的著作授權有別於 GPLBSD 的原故,在此也只能說樹莓派上的安裝辦法,以饗讀者的了。

─── 摘自《音樂播放器之 CD 轉成 mp3 之《補充》 MAME?!

 

『原始碼』 source code 是唯一最詳實文件的哩??那麼就讀碼嘗試的吧!!

eeprog.c

/***************************************************************************
copyright : (C) by 2003-2004 Stefano Barbato
email : stefano@codesink.org

Copyright (C) 2011 by Kris Rusocki <kszysiu@gmail.com>
– usage/strings cleanup
– misc cleanup: use “static” keyword where appropriate
– misc cleanup: have confirm_action() return void
– support custom input and output files
– support user-defined write cycle time
– ensure that stdin is a terminal when operating w/o -f
– do not allow reading data from a terminal w/o -f
– perform complete input validation before taking action
– use dedicated exit code when opening I2C device fails

Id: eeprog.c,v 1.28 2004/02/29 11:06:41 tat Exp
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

 

#define die_if3(a, msg, code) do { do_die_if( a , msg, __LINE__, code); } while(0)
#define die_if(a, msg) die_if3(a, msg, 1)
static void do_die_if(int b, char* msg, int line, int exitcode)
{
if(!b)
return;
fprintf(stderr, “Error at line %d: %s\n”, line, msg);
//fprintf(stderr, ” sysmsg: %s\n”, strerror(errno));
exit(exitcode);
}

 

static int read_from_eeprom(struct eeprom *e, FILE *fp, int addr, int size, int hex)
{
int ch, i;
for(i = 0; i < size; ++i, ++addr)
{
die_if((ch = eeprom_read_byte(e, addr)) < 0, “read error”);
if(hex)
{
if( (i % 16) == 0 )
fprintf(fp, “\n %.4x| “, addr);
else if( (i % 8) == 0 )
fprintf(fp, ” “);
fprintf(fp, “%.2x “, ch);
} else
putc(ch, fp);
}
if(hex)
fprintf(fp, “\n\n”);
fflush(fp);
return 0;
}

static int write_to_eeprom(struct eeprom *e, FILE *fp, int addr)
{
int c;
while((c = fgetc(fp)) != EOF)
{
print_info(“.”);
fflush(stdout);
die_if(eeprom_write_byte(e, addr++, c), “write error”);
}
print_info(“\n\n”);
return 0;

 

pi@raspberrypi:~/hats/eepromutils/eeprog/eeprog-0.7.6-tear12 ./eeprog -h eeprog 0.7.6-tear12, a 24Cxx EEPROM reader/writer Copyright (c) 2003-2004 by Stefano Barbato - All rights reserved. Copyright (c) 2011 by Kris Rusocki - All rights reserved. Usage: 	eeprog [-fqxd] [-16|-8] -r addr[:count] [-o file] /dev/i2c-N  i2c-address 	eeprog [-fqd] [-16|-8] -w addr [-i file] [-t tWC] /dev/i2c-N  i2c-address 	eeprog -h    Address modes:  	-8		Use 8bit address mode for 24c0x...24C16 [default] 	-16		Use 16bit address mode for 24c32...24C256   Actions:  	-r addr[:count]	Read [count] (1 if omitted) bytes from [addr] 			and print them to the standard output (or file 			specified by -o) 	-w addr		Write stdin (or file specified by -i) starting 			at address [addr] of the EEPROM 	-h		Print this help   Options:  	-i file		Read input from [file] (for use with -w) 	-o file		Write output to [file] (for use with -r) 	-x		Set hex output mode 	-d		Dummy mode, display what *would* have been done 	-f		Disable warnings and don't ask confirmation 	-q		Quiet mode 	-t tWC		Define chip's write cycle time to [tWC] miliseconds  The following environment variables could be set instead of the command line arguments: 	EEPROG_DEV		device name(/dev/i2c-N) 	EEPROG_I2C_ADDR		i2c-address  	Examples 	1- read 64 bytes from the EEPROM at address 0x54 on bus 0 starting 	   at address 123 (decimal) 		eeprog /dev/i2c-0 0x54 -r 123:64 	2- prints the hex codes of the first 32 bytes read from bus 1  	   at address 0x22 		eeprog /dev/i2c-1 0x51 -x -r 0x22:0x20 	3- write the current timestamp at address 0x200 of the EEPROM on  	   bus 0 at address 0x33  		date | eeprog /dev/i2c-0 0x33 -w 0x200 -f  [line 250] pi@raspberrypi:~/hats/eepromutils/eeprog/eeprog-0.7.6-tear12 

 

pi@raspberrypi:~/hats/eepromutils/eeprog/eeprog-0.7.6-tear12 $ sudo modprobe i2c_dev

pi@raspberrypi:~/hats/eepromutils/eeprog/eeprog-0.7.6-tear12 $ sudo dtoverlay i2c-gpio i2c_gpio_sda=0 i2c_gpio_scl=1

pi@raspberrypi:~/hats/eepromutils/eeprog/eeprog-0.7.6-tear12 $ ./eeprog -f -o test.bin -16 -r 0:4096 /dev/i2c-3 0x50
eeprog 0.7.6-tear12, a 24Cxx EEPROM reader/writer
Copyright (c) 2003-2004 by Stefano Barbato – All rights reserved.
Copyright (c) 2011 by Kris Rusocki – All rights reserved.
Bus: /dev/i2c-3, Address: 0x50, Mode: 16bit
Operation: read 4096 bytes from offset 0, Output file: test.bin
Reading 4096 bytes from 0x0

pi@raspberrypi:~/hats/eepromutils/eeprog/eeprog-0.7.6-tear12 $ hexedit test.bin

test

 

pi@raspberrypi:~/hats/eepromutils/eeprog/eeprog-0.7.6-tear12 $ ./eeprog -f -i test.bin -16 -w 0 -t 5 /dev/i2c-3 0x50
eeprog 0.7.6-tear12, a 24Cxx EEPROM reader/writer
Copyright (c) 2003-2004 by Stefano Barbato – All rights reserved.
Copyright (c) 2011 by Kris Rusocki – All rights reserved.
Bus: /dev/i2c-3, Address: 0x50, Mode: 16bit
Operation: write at offset 0, Input file: test.bin
Write cycle time: 5 milliseconds
Writing test.bin starting at address 0x0
…………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………

 

pi@raspberrypi:~/hats/eepromutils $ sudo ./eepflash.sh -r -f=test-ref.eep -t=24c32
This will attempt to talk to an eeprom at i2c address 0x50. Make sure there is an eeprom at this address.
This script comes with ABSOLUTELY no warranty. Continue only if you know what you are doing.
Do you wish to continue? (yes/no): yes
Reading…
8+0 records in
8+0 records out
4096 bytes (4.1 kB) copied, 0.244026 s, 16.8 kB/s
Done.
pi@raspberrypi:~/hats/eepromutils $ hexedit test-ref.eep

test-ref