Last active
July 19, 2021 18:38
-
-
Save misodengaku/371959f58f32fd1d66cbbced3915db4b to your computer and use it in GitHub Desktop.
Honda Beat(PP1) ROM mod tool
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
from minmaxcheck import MinMaxCheck | |
import sys | |
import struct | |
import matplotlib | |
import numpy as np | |
from mpl_toolkits.mplot3d import Axes3D | |
matplotlib.use('TKagg') | |
import matplotlib.pyplot as plt | |
from matplotlib import cm | |
from bullet import Bullet | |
from minmaxcheck import MinMaxCheck | |
''' | |
スピードリミット値計算メモ: | |
スピードリミット値が小さくなるほどリミッターが発動する速度が上がるようなので、スピードリミット値は車速パルスの周期に対応するものと想定される。 | |
仮にスピードリミット値を車速パルスが4発(駆動軸1回転分?)入るまでを100[us]単位で表した時間として考えてみる。 | |
純正スピードリミット値(約140km/hでリミット): 0x0654 == 1620 | |
1620 * 100[us] = 0.162[s] | |
1パルスあたり周期は 0.162/4 = 0.0405[s] | |
周波数に直すと 1/0.0405[s] = 24.7[Hz] | |
1分間に出力されるパルス数は 24.7 * 60 = 1481.5 | |
60[km/h]のとき637[ppm]でパルスが出力される(JIS D5601)。 | |
1分間に出力されるパルス数を637で割ると、60[km/h]に対する実際の車速の係数が得られるはず。上記数値を当てはめてみると | |
1481.5 / 637 = 2.33 | |
60[km/h] * 2.33 = 139.8[km/h] | |
参考文献 | |
# ref: https://minkara.carview.co.jp/userid/526128/car/457100/2454111/note.aspx | |
# ref: https://minkara.carview.co.jp/userid/526128/car/457100/3397385/note.aspx | |
''' | |
def bin2speed(b): | |
x = struct.unpack('<H', b)[0] | |
return (1 / (x / 10000 / 4) * 60 / 637 * 60) | |
def speed2bin(v): | |
x = struct.pack('<H', int(40000 / (v * 637 / 3600))) | |
return x[0:2] | |
class ECUParser: | |
def __init__(self, fname): | |
self.has_unsaved_value = False | |
self.load_ecu_rom(fname=fname) | |
def create_fig(self, m, title=''): | |
fig = plt.figure() | |
fig.canvas.manager.set_window_title(title) | |
ax = Axes3D(fig, auto_add_to_figure=False) | |
fig.add_axes(ax) | |
# ax = fig.add_subplot(111, projection='3d') | |
X, Y = np.mgrid[0:17, 0:15] | |
na = np.array(m).reshape(17, 15) | |
# print(na) | |
# X, Y = np.meshgrid(na.shape[0], na.shape[1]) | |
ax.plot_surface(X, Y, na, rstride=1, cstride=1, cmap=cm.coolwarm) | |
ax.set_xlabel('rev') | |
ax.set_ylabel('load') | |
plt.title(title) | |
return plt | |
def parse_ecu_rom(self): | |
# ref: https://web.archive.org/web/20150516162622/http://www.h3.dion.ne.jp/~beat_ecu/ecu4.html | |
self.fi_cut_speed = bin2speed(self.rom_binary[0x0A59:0x0A5B]) | |
self.fi_restart_speed = bin2speed(self.rom_binary[0x0A61:0x0A63]) | |
self.load_axis = struct.unpack('<15B', self.rom_binary[0x3C8A:0x3C8A+15]) | |
self.rev_axis = struct.unpack('<17B', self.rom_binary[0x3C99:0x3C99+17]) | |
# print(self.load_axis, sum(self.load_axis)) | |
# print(self.rev_axis, sum(self.rev_axis)) | |
self.ig_timing_map = [] | |
for i in range(0x3CA9, 0x3CA9 + 15*17): | |
self.ig_timing_map.append(self.rom_binary[i]) | |
self.th_speed_density_map = [] | |
for i in range(0x3DC6, 0x3DC6 + 15*17): | |
self.th_speed_density_map.append(self.rom_binary[i]) | |
self.d_jetronic_map = [] | |
for i in range(0x3EF2, 0x3EF2 + 15*17): | |
self.d_jetronic_map.append(self.rom_binary[i]) | |
# print(ig_timing_map) | |
# print(th_speed_density_map) | |
# print(d_jetronic_map) | |
def load_ecu_rom(self, fname): | |
self.has_unsaved_value = False | |
self.rom_binary = bytearray(open(fname, 'rb').read()) | |
self.parse_ecu_rom() | |
def show_ecu_info(self): | |
self.parse_ecu_rom() | |
print('fi_cut: %.1f [km/h]' % self.fi_cut_speed) | |
print('fi_restart: %.1f [km/h]' % self.fi_restart_speed) | |
def save_figs(self, ig_timing=True, dth_ne=True, pb_ne=True): | |
if ig_timing: | |
p = self.create_fig(self.ig_timing_map, title='Ignition Timing') | |
p.savefig('ig_timing.png') | |
print('ig_timing.png saved.') | |
if dth_ne: | |
p = self.create_fig(self.th_speed_density_map, title='θTH-Ne Map') | |
p.savefig('th_speed_density_map.png') | |
print('th_speed_density_map.png saved.') | |
if pb_ne: | |
p = self.create_fig(self.d_jetronic_map, title='Pb-Ne(D-jetronic) Map') | |
p.savefig('d_jetronic_map.png') | |
print('d_jetronic_map.png saved.') | |
def show_figs(self, ig_timing=True, dth_ne=True, pb_ne=True): | |
if ig_timing: | |
print('in showing: "Ignition Timing"') | |
p = self.create_fig(self.ig_timing_map, title='Ignition Timing') | |
p.show() | |
if dth_ne: | |
print('in showing: "θTH-Ne Map"') | |
p = self.create_fig(self.th_speed_density_map, title='θTH-Ne Map') | |
p.show() | |
if pb_ne: | |
print('in showing: "Pb-Ne(D-jetronic) Map"') | |
p = self.create_fig(self.d_jetronic_map, title='Pb-Ne(D-jetronic) Map') | |
p.show() | |
def change_speed_limit(self, fi_cutoff=140, fi_restart=137): | |
self.has_unsaved_value = True | |
backup_fi_cutoff = self.rom_binary[0x0A59:0x0A5B] | |
x = speed2bin(fi_cutoff) | |
self.rom_binary[0x0A59] = x[0] | |
self.rom_binary[0x0A5A] = x[1] | |
backup_fi_restart = self.rom_binary[0x0A59:0x0A5B] | |
x = speed2bin(fi_restart) | |
self.rom_binary[0x0A61] = x[0] | |
self.rom_binary[0x0A62] = x[1] | |
checksum_diff = 0 | |
checksum_diff = checksum_diff + backup_fi_cutoff[0] - self.rom_binary[0x0A59] + backup_fi_cutoff[1] - self.rom_binary[0x0A5A] | |
checksum_diff = checksum_diff + backup_fi_restart[0] - self.rom_binary[0x0A61] + backup_fi_restart[1] - self.rom_binary[0x0A62] | |
print("checksum diff: %02X" % (checksum_diff & 0xFF)) | |
filename = 'beat_ecu_stock.BIN' | |
ecu = ECUParser(filename) | |
while True: | |
print('--------------------') | |
if ecu.has_unsaved_value: | |
print('Source: *%s (unsaved)' % filename) | |
else: | |
print('Source: %s' % filename) | |
cli = Bullet( | |
prompt = "Main menu: ", | |
margin = 4, | |
choices = ["Show ECU information", "Change speed limit", "Save map plots", "Show map plots", "Save modified ROM data", "Exit"] | |
) | |
result = cli.launch() | |
if result == 'Exit': | |
sys.exit(0) | |
print('--------------------') | |
print('**** execute "%s"' % result) | |
if result == "Show ECU information": | |
ecu.show_ecu_info() | |
elif result == "Change speed limit": | |
while True: | |
print('Enter speed of fuel cutoff point [km/h] => ') | |
try: | |
fi_cutoff = float(input()) | |
print('OK: fuel cutoff: %.1f [km/h]' % fi_cutoff) | |
break | |
except: | |
print('Invalid value.') | |
continue | |
while True: | |
print('Enter speed of limiter release [km/h] => ') | |
try: | |
fi_release_limit = float(input()) | |
print('OK: limit release: %.1f [km/h]' % fi_release_limit) | |
break | |
except: | |
print('Invalid value.') | |
continue | |
ecu.change_speed_limit(fi_cutoff=fi_cutoff, fi_restart=fi_release_limit) | |
elif result == "Save map plots": | |
cli = MinMaxCheck( | |
prompt = "press Space to choose map(s), and press Enter to execute save:", | |
margin = 4, | |
min_selections=0, | |
max_selections=3, | |
choices = ["Ignition Timing", "θTH-Ne", "Pb-Ne(D-jetronic)"] | |
) | |
result = cli.launch() | |
if len(result) > 0: | |
ecu.save_figs(ig_timing="Ignition Timing" in result, dth_ne="θTH-Ne" in result, pb_ne="Pb-Ne(D-jetronic)" in result) | |
else: | |
print('nothing to do.') | |
elif result == 'Show map plots': | |
cli = MinMaxCheck( | |
prompt = "press Space to choose map(s): ", | |
margin = 4, | |
min_selections=0, | |
max_selections=3, | |
choices = ["Ignition Timing", "θTH-Ne", "Pb-Ne(D-jetronic)"] | |
) | |
result = cli.launch() | |
if len(result) > 0: | |
ecu.show_figs(ig_timing="Ignition Timing" in result, dth_ne="θTH-Ne" in result, pb_ne="Pb-Ne(D-jetronic)" in result) | |
else: | |
print('nothing to do.') | |
elif result == 'Write modified ROM data': | |
if not ecu.has_unsaved_value: | |
print('nothing to do.') | |
continue | |
# x = 0 | |
# y = 0 | |
# for i in range(0, 0x8000): | |
# x = x ^ self.rom_binary[i] | |
# y = (y + self.rom_binary[i]) & 0xff | |
# if x == self.rom_binary[i]: | |
# print("%02X" % x) | |
# print("xor match %02X" % i) | |
# if y == self.rom_binary[i]: | |
# print("%02X" % y) | |
# print("sum match %02X" % i) |
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
''' | |
original: https://github.com/bchao1/bullet/blob/master/examples/check.py | |
MIT License | |
Copyright (c) 2019 Brian Chao | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
''' | |
from bullet import Check, keyhandler, styles | |
from bullet.charDef import NEWLINE_KEY | |
class MinMaxCheck(Check): | |
def __init__(self, min_selections=0, max_selections=None, *args, **kwargs): | |
super().__init__(*args, **kwargs) | |
self.min_selections = min_selections | |
self.max_selections = max_selections | |
if max_selections is None: | |
self.max_selections = len(self.choices) | |
@keyhandler.register(NEWLINE_KEY) | |
def accept(self): | |
if self.valid(): | |
return super().accept() | |
def valid(self): | |
return self.min_selections <= sum(1 for c in self.checked if c) <= self.max_selections |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment