Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed BFINS instruction when width+offset > 32 #87

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jotd666
Copy link
Contributor

@jotd666 jotd666 commented Mar 13, 2022

This is a corner case of BFINS instruction when width + offset > 32 (width = 0 makes it 32 too)

To test :

D0 = $275

A0 = some address

BFINS D0,(a0){4:0} => A0 holds: X0000027, A0+4 holds: 5XXXXXXX

Without the correction, second longword is incorrect because shifting isn’t taken into account and we get

A0 holds: X0000027, A0+4 holds: 75XXXXXX

UAE core does that shifting, musashi did not. Relevant uae code:

   if (((offset & 7) + width) > 32) {

        bf1 = (bf1 & (0xff >> (width - 32 + (offset & 7)))) |

        (tmp << (8 - (offset & 7)));  <-- here the shifting with offset is performed

        put_byte(dsta+4,bf1);

We had this issue in our application and that change fixed functionnal behaviour

BFTST and BFSET and BFCLR instructions probably have the same issue, but we're not using them in our application with a width > 32 so we didn't took the time to work on a fix.

@agentbooth
Copy link
Contributor

For reference, the MAME code for this block is:
https://github.com/mamedev/mame/blob/54442a7a5b4b69ea667a5ce1051d454bb5d22f43/src/devices/cpu/m68000/m68k_in.lst#L1786-L1792

	if((width + offset) > 32) {
		mask_byte = MASK_OUT_ABOVE_8(mask_base) << (8-offset);
		insert_byte = MASK_OUT_ABOVE_8(insert_base) << (8-offset);
		data_byte = m68ki_read_8(ea+4);
		m_not_z_flag |= (insert_byte & mask_byte);
		m68ki_write_8(ea+4, (data_byte & ~mask_byte) | insert_byte);
	}

Should that mask_byte line also be updated?
mask_byte = MASK_OUT_ABOVE_8(mask_base) << (8-offset);

@jotd666
Copy link
Contributor Author

jotd666 commented Mar 22, 2022 via email

@agentbooth
Copy link
Contributor

That sounds like a good plan. You will probably find some other fixes in the MAME code that could be helpful.

Yes, it is too bad some of the improvements in MAME were never backported to Musashi. But at this point, there seems to be a significant divergence as to how some of the code is written between the two. But something like these bitfield fixes could have been backported fairly easily.

@jotd666
Copy link
Contributor Author

jotd666 commented Mar 22, 2022 via email

@jotd666
Copy link
Contributor Author

jotd666 commented Mar 23, 2022 via email

@agentbooth
Copy link
Contributor

This is great work that you are doing and would be a real asset to get moved back into musashi!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants