question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

M68000: Decompiler (sbcd instruction)

See original GitHub issue

Decompiler does not understand sbcd instruction: изображение

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
emteerecommented, Mar 21, 2019

These may not be exactly right, but will get past issues with the unimplemented instructions. Feel free to improve. If they are acceptable in this form, then they’ll be in the next release. In writing semantics the most important thing is to get the inputs and affected registers and memory for data flow. These semantics could be too complicated for the needed use in understanding the code. Many instructions will never have the full semantics, such as sin().

diff --git a/Ghidra/Processors/68000/data/languages/68000.sinc b/Ghidra/Processors/68000/data/languages/68000.sinc
index aab6335..a20600c 100644
--- a/Ghidra/Processors/68000/data/languages/68000.sinc
+++ b/Ghidra/Processors/68000/data/languages/68000.sinc
@@ -306,6 +306,8 @@
 define pcodeop restoreFPUStateFrame;
 define pcodeop pushInvalidateCaches;
 
+define pcodeop bcdAdjust;
+
 define pcodeop sin;
 define pcodeop cos;
 define pcodeop tan;
@@ -638,6 +640,11 @@
  $(NAN_FP) = 0;
 }
 
+macro bcdflags(result) {
+ XF = CF;
+ ZF = (result == 0) * ZF + (result != 0);
+}
+
 # SCR 10997:
 macro getbit(res,in,bitnum) {
   res = ((in >> bitnum) & 1) != 0;
@@ -687,7 +694,7 @@
 
 with : extGUARD=1 {
 
-:abcd Tyb,Txb			is op=12 & op48=16 & Tyb & Txb					unimpl
+:abcd Tyb,Txb			is op=12 & op48=16 & Tyb & Txb					{ CF = carry(Tyb,carry(Txb,XF)); Tyb = Tyb + Txb + XF; Tyb = bcdAdjust(Tyb); bcdflags(Tyb); }
 
 :add.b eab,reg9dnb		is (op=13 & reg9dnb & op68=0)... & eab				{ addflags(eab,reg9dnb); reg9dnb = reg9dnb + eab; resflags(reg9dnb); }
 :add.w eaw,reg9dnw		is (op=13 & reg9dnw & op68=1)... & eaw				{ addflags(eaw,reg9dnw); reg9dnw = reg9dnw + eaw; resflags(reg9dnw); }
@@ -1391,7 +1398,8 @@
 											  regdr=res(4); resflags(res); CF=0; VF=0; }
 :mul^mulsize e2l,submul		is opbig=0x4c & op67=0 & $(DAT_ALTER_ADDR_MODES); submul & mulsize; e2l [ savmod2=savmod1; regtsan=regtfan; ] { glbdenom=e2l; build submul; }
 
-:nbcd eab			is (opbig=0x48 & op67=0 & $(DAT_ALTER_ADDR_MODES))... & eab			unimpl
+:nbcd eab			is (opbig=0x48 & op67=0 & $(DAT_ALTER_ADDR_MODES))... & eab
+                   {  tmp:1 = eab; CF = (tmp != 0) || (XF == 1);  tmp = 0 - tmp - XF; eab = bcdAdjust(tmp); bcdflags(tmp); }
 
 
 # NB: For the neg insn the CF carry flag is not set like other insns, from the manual:
@@ -1600,7 +1608,8 @@
 
 :rts				is opbig=0x4e & op37=14 & op02=5				{ PC = *SP; SP = SP+4; return [PC]; }
 
-:sbcd Tyb,Txb			is op=8 & op48=16 & Txb & Tyb					unimpl
+:sbcd Tyb,Txb			is op=8 & op48=16 & Txb & Tyb
+                    {  CF = (Tyb < Txb) || ( (XF == 1) && (Tyb == Txb) );  Tyb = Tyb - Txb - XF; Tyb = bcdAdjust(Tyb); bcdflags(Tyb); }
 
 :s^cc eab			is (op=5 & cc & op67=3 & $(DAT_ALTER_ADDR_MODES))... & eab				{ eab = -cc; }
 

P.S. That’s alot of red bookmarks in your code browser. Hopefully this will fix some of them.

0reactions
emteerecommented, Apr 12, 2019

Hmmm. Thought these made it in. Will check on it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The 68000's Instruction Set
The ABCD instruction is used in chain arithmetic to add together strings of BCD digits. Consider the addition of two nine-digit numbers.
Read more >
Motorola M68000 FAMILY Programmer's Reference Manual
This manual contains detailed information about software instructions used by the microprocessors and coprocessors in the M68000 family, including: MC68000.
Read more >
SAMS_68000_68010_68020_Pr...
The 68000 Instruction Summary Table - Preliminaries 310. E M68000 Resources ... code (traditionally, one blames the computer) and decompiling your own stuff....
Read more >
Emulation & Decompilation: Time Keeping - Reassembler
The 68k code uses the bcs (branch if carry set) instruction to test if the sbcd instruction has caused a carry by subtracting...
Read more >
A 68000 Disassembler - Jeff Tranter's Blog
This is more challenging than for 8-bit processors due to it's complex instruction set and many addressing modes. I decided to again use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found