Trim codes, especially if they've been long term issues, can damage 02 sensors and cats, so I hope you haven't been ignoring stuff for a real long time.
Check for air leaks first. Hoses are inexpensive to fix, just a little time consuming to find.
Yes, I think that trim codes might be why you failed emissions. Read jfoj's sig link. Search for jfoj and read the link he has on 'solving misfires'
Upcoming comedy shows: March 3 at Broadway Comedy Club, NYC, 8 p.m. March 5, Tierney's Tavern, Montclair, NJ, 7:30 p.m. See Facebook for other shows and PM me if you can make one...love to see you there!
Mangofy your E46!